Laravel 5.8 - Can we make Sync in a pivot table without the id field?

Asked

Viewed 504 times

1

I have a table pivot with the countryside user_id and article_id (much for many), let’s say I have the following records:

1-2,
1-3,
1-4,
2-1,
2-2.

At a certain point user 1 no longer wants to be connected to article 2 and also adds a relation to article 5, so I would have 2 processes: delete the relation 1-2 and add the relationship 1-5, note that in the table there is no id field.

How it’s done using sync? Looking at the documentation it seems to me that sync only works when we have the id field in the table pivot?

There would be no way to delete using where user_id=1 AND article_id=2?

2 answers

3


Laravel 5.8 - We can do sync in a table pivot without the field id?

The Eloquent ORM Framework contained in Laravel has this command Sync which has in its documentation the following text:

  • Using Sync To Attach Many To Many Models

    You may also use the Sync method to attach Related models. The Sync method accepts an array of Ids to place on the pivot table. After this Operation is complete, only the Ids in the array will be on the Intermediate table for the model:

google translation:

  • Using synchronization to connect many to many models

    You can also use the synchronization method to attach related templates. The synchronization method accepts an array of Ids to place in the dynamic table. After the completion of the operation, only the Ids in the matrix will be in the intermediate table of the model:

That is, in a ratio of many to many the function of this method is to adjust the ratio in the table pivot where the codes to be deleted and the codes to be inserted are made transparently. In a real analogy is done in the method Sync one detach(that removes registration from the relationship) and then a attach(which adds registration to the relation).

In the relation table, the model presented in the documentation does not show id, nor is it necessary, maybe extra fields with some kind of configuration can coexist, but the primary key composed of the two relations.

Example:

An author may be contained in several books and vice versa book may have several authors:

class Autor extends Model
{

    protected $table = 'authors';
    protected $primaryKey = 'id';
    protected $fillable = ['nome'];

    //Relacionamento. N:M
    public function livros()
    {        
        return $this->belongsToMany('App\Livro',
            'autor_livros', 
            'autor_id', 
            'livro_id'
        );
    }
}

class Livro extends Model
{    
    protected $table = 'books';
    protected $primaryKey = 'id';
    protected $fillable = ['titulo'];
    public  $timestamps = false;

    //Relacionamento. N:M
    public function autores()
    {
        return $this->belongsToMany('App\Autor',
            'autor_livros',
            'livro_id',
            'autor_id'
        );
    }
}

Using the method Sync:

$livro = Livro::find(1);
$livro->autores()->sync(array(1,2));

or

$autor = Autor::find(1);
$autor->livros()->sync(array(3,4));

Ref.

0

You can tell in your pivot table model which primary key, as below:

class PivotModel extends Eloquent {
   public $incrementing = false;
   protected $primaryKey = [
    'user_id',
    'article_id'
   ];
}

After that just do a query by removing from the user_id and article_id, as below:

$item = Pivot::where('article_id', 1)->where('user_id', 2)->first();
$item->delete();

Another option is to use the methods attach and detach as follows:

$user = App\User::find(1);

$user->roles()->detach(3);
$user->roles()->attach(5);
  • i don’t have model for pivot table. I could create it. But vc has some suggestion using Sync method and no model?

  • I put using attach and detach, I believe it is simpler than using Sync in this case.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.