Laravel, sync() - how to sync an array and also pass additional pivot fields?

匿名 (未验证) 提交于 2019-12-03 02:52:02

问题:

Official Laravel documentation has this on sync() function:

$user->roles()->sync( array( 1, 2, 3 ) ); 

You may also associate other pivot table values with the given IDs:

$user->roles()->sync( array( 1 => array( 'expires' => true ) ) ); 

In the latter example only a single pivot row is being added. What I don't understand is how can I associate other pivot table records if there are more than one rows to be synced?

Thanks in advance.

回答1:

In order to sync multiple models along with custom pivot data, you need this:

$user->roles()->sync( array(      1 => array( 'expires' => true ),     2 => array( 'expires' => false ),     ... )); 

Ie.

sync( array(      related_id => array( 'pivot_field' => value ),     ... )); 

edit

Answering the comment:

$speakers  = (array) Input::get('speakers'); // related ids $pivotData = array_fill(0, count($speakers), ['is_speaker' => true]); $syncData  = array_combine($speakers, $pivotData);  $user->roles()->sync($syncData); 


回答2:

This works for me

foreach($photos_array as $photo_name){  //collect all inserted record IDs $photo_id_array[$photo->id] = ['type' => 'Offence'];  }  //Insert into offence_photo table $offence->photos()->sync($photo_id_array, false);//dont delete old entries = false 


回答3:

Attaching / Detaching

Eloquent also provides a few additional helper methods to make working with related models more convenient. For example, let's imagine a user can have many roles and a role can have many users. To attach a role to a user by inserting a record in the intermediate table that joins the models, use the attach method:

$user = App\User::find(1);  $user->roles()->attach($roleId); 

When attaching a relationship to a model, you may also pass an array of additional data to be inserted into the intermediate table:

$user->roles()->attach($roleId, ['expires' => $expires]); 

You can also use Sync if you want to remove old roles and only keep the new ones you are attaching now

$user->roles()->sync([1 => ['expires' => $expires], 2 => ['expires' => $expires]); 

The default behaviour can be changed by passing a 'false' as a second argument. This will attach the roles with ids 1,2,3 without affecting the existing roles.

In this mode sync behaves similar to the attach method.

$user->roles()->sync([1 => ['expires' => $expires], 2 => ['expires' => $expires], false); 

Reference: https://laravel.com/docs/5.4/eloquent-relationships



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!