2

I would like to observe a pivot table in which, rows are created with an attach method in a specific model, is there a way to Observe that pivot table through the attach method that is responsible for creating rows?

Abbas Fechit
  • 111
  • 8
  • I don't think you can observe the attach method itself but you can always make a [custom pivot model](https://laravel.com/docs/8.x/eloquent-relationships#defining-custom-intermediate-table-models) and attach observers on the appropriate events on that – apokryfos Jan 22 '22 at 14:09
  • I have answered myself with the solution, thank you for your answer. – Abbas Fechit Jan 22 '22 at 15:48

2 Answers2

6

after struggling some time, I came to answer my question,

so in order to observe a table whose rows are created by the attach method, we will need to do 3 things

1- we will need to create a model that extends $Illuminate\Database\Eloquent\Relations\Pivot

2- Connect the model to the database table with this line:

protected $table = 'data_base_table_name';

3- use the method 'using' at the end of the BelongsToMany relationship in each model that is related to the pivot table

Example:

let's say we have a model called Student and another one called Group, we have also a pivot table called group_students that is filled with the attach method since we have a student BelongsToMany groups and Group BelongsToMany Students,

we will need to create a model named GroupStudent that extends Illuminate\Database\Eloquent\Relations\Pivot and link it to the group_students by adding the following line in the GroupStudent Class:

protected $table = 'group_student'

After that, we will need to add the using method The BelongsToMany relations in the Student Model and the Group Model like the following:

public function students()
{
    return $this->BelongsToMany(Student::class)->using(GroupStudent::class);
}

and

public function groups()
{
    return $this->belongsToMany(Group::class)->using(GroupStudent::class);
}

And here we go, now whenever I create a row in the group_students table through the attach method, this will be observed and the method created will be executed.

Abbas Fechit
  • 111
  • 8
  • In quite a few circumstances, you'll also need to use `->withPivot('id')` or whatever your primary key column is on the pivot table, and potentially other fields depending on your needs. Laravel/Eloquent doesn't not automatically select all pivot table fields, even if you're using a column pivot model that it return from `$relation->pivot`. That pivot object won't contain any of the attributes that aren't selected in `->withPivot()`. Apparently it's complicated, but it's a brutal concept to have to remember. – Joel Mellon Mar 04 '22 at 21:24
0

Anyone looking - looks like this explains Laravel 5.8+ supports this: Eloquent attach/detach/sync fires any event?

dfeva
  • 81
  • 2
  • 11
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 25 '23 at 07:34