雄弁な関係チュートリアル


これは私たちのラーラベル8 -チュートリアルチュートリアルのパート1です

セットアップ
Laravelアプリerdアプリを作成しましょう
composer create-project laravel/laravel erd-app
cd erd-app
オープン.envとローカルデータベースに接続php artisan serveそれから、学生とプロフィールデータベースをつくってくださいphp artisan make:migration create_students_tableアプリケーション/データベース/移行に移動します
Schema::create('students', function (Blueprint $table) {
            $table->id();
            $table->string('first_name');
            $table->string('last_name');
            $table->timestamps();
        });
コピーと保存php artisan make:migration create_profiles_tableアプリケーション/データベース/移行に移動します
Schema::create('profiles', function (Blueprint $table) {
            $table->id();
            $table->foreignId('student_id');
            $table->string('email');
            $table->string('phone');
            $table->timestamps();
        });
コピーと保存
マイグレーション表を作成するphp artisan migrateデータベースをチェックする
今モデル学生とプロファイルを作成しましょうphp artisan make:model Studentphp artisan make:model Profile

models should be singular while the table is plural


一対一関係
アプリ/モデルに移動して
1対1の関係を定義する
モデル/学生.PHP
protected $fillable = [
        'first_name',
        'last_name',
    ];

    public function profile()
    {
        return $this->hasOne('App\Models\Profile');
    }
モデル/プロファイルで.PHP
  protected $fillable = [
        'student_id',
        'email',
        'phone',
    ];

    public function student()
    {
        return $this->belongsTo('App\Models\Student');
    }
これは、2つのモデルの関係を定義します
コントローラとルートを作成するphp artisan make:controller StudentControllerアプリケーション/HTTP/コントローラ/studentcontrollerに行く
use App\Models\Student;

...

public function index(){
        $stu = Student::find(1);
        dd($stu);
    }

    public function store(){
        $student = new Student;
        $student->first_name = 'Dale';
        $student->last_name = 'Lanto';
        $student->save();
        dd($student);
    }
アプリケーション/ルート/ウェブのルートを作成します.PHP
Route::get('/students', [StudentController::class,'index'])->name('students');
Route::get('/students/store', [StudentController::class,'store'])->name('store');
あなたのブラウザーで、http://localhost:8000/students/storeに行って、データベースの1つのエントリーを作成してください
モデルをチェックするために
次のようになります.
http://localhost:8000/students
今すぐプロファイルの1エントリを作成できます
新しいルートを作る
Route::get('/students/store/profile', [StudentController::class,'store_profile'])->name('storeProfile');
コントローラで新しい関数を作成する
public function store_profile(){

        $student = Student::find(1);

        $profile = new Profile;
        $profile->student_id = $student->id;
        $profile->email = '[email protected]';
        $profile->phone = '7623423814';
        $profile->save();

        dd($profile);
    }
データベース内の1エントリを作成するために
インデックスを関数から更新する
public function index(){
        $student = Student::find(1);
        dd($student->profile);
    }
チェックするに行く
次のようになります.
http://localhost:8000/students/store/profile

Hurray! We have successfully created our One to One Relationship.


ベストプラクティスのためには、モデルでForeignRenKeyとLocalRangeキー/ownerRoundキーを含めることが最善です.
モデル/プロファイルに行こう.PHPをこのコードに変更します
public function student()
    {
        // return $this->belongsTo('Model', 'foreign_key', 'owner_key'); 
        return $this->belongsTo('App\Models\Student','student_id','id');
    }

学生モデルからプロファイルにアクセスしましょう
public function index(){
        $student = Student::with('profile')->get();
        dd($student);
    }
結果:
http://localhost:8000/students

Click this to proceed with the next tutorial - One to Many Relationship!