У пользователя много заметок.
Примечания принадлежат пользователю.
в модели пользователя:
public function accountingNotes()
{
return $this->hasMany(User::class,'user_id','user_id');
}
в ноте Модель:
public function user()
{
return $this->belongsTo(AccountingNote::class,'user_id','written_by');
}
Миграция пользователя
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('user_id');
// username will be used to login
// authentication and validation are done to user username to login.
$table->string('username')->unique();
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->dateTime('first_login');
$table->string('password', 60);
$table->string('first_name');
$table->string('last_name');
$table->string('phone');
$table->string('emergency_phone')->nullable();
$table->enum('gender', ['male', 'female']);
$table->string('profile_photo')->default('default.jpeg');
$table->string('role');
$table->rememberToken();
$table->timestamps();
});
}
Нота переноса бухгалтерского учета
public function up()
{
Schema::create('accounting_notes', function (Blueprint $table) {
$table->bigIncrements('accounting_note_id');
$table->text('content');
$table->unsignedBigInteger('written_by');
$table->foreign('written_by')->references('user_id')->on('users')->onDelete('CASCADE');
$table->unsignedBigInteger('timesheet_id');
$table->foreign('timesheet_id')->references('timesheet_id')->on('timesheets')->onDelete('CASCADE');
$table->timestamps();
});
}
Теперь пользователь имеет user_id
в качестве первичного ключа.
в Note model
, written_by
используется для связи с пользователем.
Что я должен заполнить foreign key and local keys
?
Во многих блогах они объясняли неправильно, и когда я попробовал несколько раз, это не сработало, и я пришел сюда, чтобы получить некоторую помощь.
Спасибо