Laravel官方文档具有以下sync()功能:
$user->roles()->sync( array( 1, 2, 3 ) );
您还可以将其他数据透视表值与给定的ID相关联:
$user->roles()->sync( array( 1 => array( 'expires' => true ) ) );
在后一个示例中,仅添加了一个枢轴行。我不明白的是,如果要同步的行多,如何关联其他数据透视表记录?
提前致谢。
Laravel官方文档具有以下sync()功能:
$user->roles()->sync( array( 1, 2, 3 ) );
您还可以将其他数据透视表值与给定的ID相关联:
$user->roles()->sync( array( 1 => array( 'expires' => true ) ) );
在后一个示例中,仅添加了一个枢轴行。我不明白的是,如果要同步的行多,如何关联其他数据透视表记录?
提前致谢。
Answers:
这对我有用
foreach ($photos_array as $photo) {
    //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
->sync($photo_id_array, false)到->syncWithoutDetaching($photo_id_array)为好。由于这是最优雅的解决方案,因此也要提出建议。
                    为了sync与自定义数据透视表一起使用多个模型,您需要:
$user->roles()->sync([ 
    1 => ['expires' => true],
    2 => ['expires' => false],
    ...
]);
就是
sync([
    related_id => ['pivot_field' => value],
    ...
]);
编辑
回答评论:
$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);
$speakers = \Input::get( 'speakers' )(其中$音箱成为一个数组),然后想通过$speakers沿=>array( 'is_speaker' => true)?
                    ids键作为构建数组,或在HTML表单中以这种方式进行构建。
                    $training->users()->sync( array( $speakers => array( 'is_speaker' => true ) ) )
                    装卸
Eloquent还提供了一些其他的辅助方法,以使使用相关模型更加方便。例如,假设一个用户可以有许多角色,而一个角色可以有许多用户。要通过在连接模型的中间表中插入一条记录来将角色附加给用户,请使用attach方法:
$user = App\User::find(1);
$user->roles()->attach($roleId);
将关系附加到模型时,您还可以传递要插入到中间表中的其他数据数组:
$user->roles()->attach($roleId, ['expires' => $expires]);
如果要删除旧角色,而仅保留现在要附加的新角色,则也可以使用“同步”
$user->roles()->sync([1 => ['expires' => $expires], 2 => ['expires' => $expires]);
可以通过传递“ false”作为第二个参数来更改默认行为。这将附加ID为1,2,3的角色,而不会影响现有角色。
在此模式下,同步的行为类似于attach方法。
$user->roles()->sync([1 => ['expires' => $expires], 2 => ['expires' => $expires], false);
将以下特征添加到项目中,并将其作为特征附加到模型类中。这很有用,因为这增加了使用多个枢轴的功能。可能有人可以清理一下并加以改善;)
namespace App\Traits;
trait AppTraits
{
    /**
     * Create pivot array from given values
     *
     * @param array $entities
     * @param array $pivots
     * @return array combined $pivots
     */
    public function combinePivot($entities, $pivots = [])
    {
        // Set array
        $pivotArray = [];
        // Loop through all pivot attributes
        foreach ($pivots as $pivot => $value) {
            // Combine them to pivot array
            $pivotArray += [$pivot => $value];
        }
        // Get the total of arrays we need to fill
        $total = count($entities);
        // Make filler array
        $filler = array_fill(0, $total, $pivotArray);
        // Combine and return filler pivot array with data
        return array_combine($entities, $filler);
    }
}
模型:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Example extends Model
{
    use Traits\AppTraits;
    // ...
}
用法:
// Get id's
$entities = [1, 2, 3];
// Create pivots
$pivots = [
    'price' => 634,
    'name'  => 'Example name',
];
// Combine the ids and pivots
$combination = $model->combinePivot($entities, $pivots);
// Sync the combination with the related model / pivot
$model->relation()->sync($combination);
只需将您的字段及其值附加到元素即可:
$user->roles()->sync([
   1 => ['F1' => 'F1 Updated']
]);
$data = array();
foreach ($request->planes as $plan) {
 $data_plan = array($plan => array('dia' => $request->dia[$plan] ) );                
 array_push($data,$data_plan);    
}
$user->planes()->sync($data);