雄弁な関係チュートリアル
これは私たちのラーラベル8 -チュートリアルチュートリアルのパート1です
セットアップ
Laravelアプリerdアプリを作成しましょう
マイグレーション表を作成する
今モデル学生とプロファイルを作成しましょう
一対一関係
アプリ/モデルに移動して
1対1の関係を定義する
モデル/学生.PHP
コントローラとルートを作成する
モデルをチェックするために
次のようになります.
http://localhost:8000/students
今すぐプロファイルの1エントリを作成できます
新しいルートを作る
インデックスを関数から更新する
次のようになります.
http://localhost:8000/students/store/profile
ベストプラクティスのためには、モデルでForeignRenKeyとLocalRangeキー/ownerRoundキーを含めることが最善です.
モデル/プロファイルに行こう.PHPをこのコードに変更します
http://localhost:8000/students
セットアップ
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 Student
php 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);
}
アプリケーション/ルート/ウェブのルートを作成します.PHPRoute::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!
Reference
この問題について(雄弁な関係チュートリアル), 我々は、より多くの情報をここで見つけました https://dev.to/dalelantowork/laravel-eloquent-relationships-advanced-notes-2bg2テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol