eloquent - How can I store two foreign keys from the users table into a second table in Laravel 5.4? -


i stuck new laravel.

i have users table , message table , here message table schema

schema::create('messages', function (blueprint $table) {         $table->increments('id');         $table->integer('sender')->unsigned();         $table->foreign('sender')->references('id')->on('users')->ondelete('cascade');         $table->integer('reciever')->unsigned();         $table->foreign('reciever')->references('id')->on('users')->ondelete('cascade');         $table->string('msg');         $table->string('attachment')->nullable();         $table->string('seen')->default(0);         $table->integer('deleted')->default(0);         $table->timestamps();     }); 

here sender , reciever both foriegn keys of user table.

in app have access sender id auth::user() , have access reciever id.. how can estublish relationship , store messages? have been trying in many ways please help. thank you.

i think problem relationship. please have , suggest me right way

the user model

public function sender() {    return $this->hasmany('app\message', 'sender'); }  public function reciever() {    return $this->hasmany('app\message', 'reciever'); } 

the message model

public function sender() {    return $this->belongsto('app\user', 'sender'); }  public function reciever() {    return $this->belongsto('app\user', 'reciever'); } 

you may this:

// create message model instance $message = new app\message(['msg' => 'something test', ...]);  // retrieve related user $user = app\user::find(1);  // call method defined in user model $user->sender()->save($message); 

Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -