Considering your tables are users, signatures and user_signatures

Define the relation in model: User.php

class User extends Model {
   public function signatures() {
       return $this->belongsToMany('\App\Signature', 'user_signatures');

Define the inverse of the relation in model: Signature.php

class Signature extends Model {    
   public function users() {
       return $this->belongsToMany('\App\User', 'user_signatures');

In your controller you could do the following:

//create a new signature
$signature = new Signature($values);
//save the new signature and attach it to the user
$user = User::find($id)->signatures()->save($signature);

The opposite is possible too:

$user = User::find($user_id);
$signature = Signature::create($values)->users()->save($user);

Alternatively if you have an existing signature you should do:

$user = User::find($id);

Please note that attach() and detach() accepts an id or an array of ids.

Laravel docs has covered this in much more details and better examples. You should check attach(), detach() and sync() methods. Please have a look:

Добавить комментарий

Ваш e-mail не будет опубликован. Обязательные поля помечены *

Любишь мемасики?

Подпишись на мой телеграм-канал!