我正在使用webpatser / laravel-uuid。生成uuid到我的数据透视表中,效果很好,但是问题是每次执行循环时都会重复生成的字符串,我不知道该如何处理。
控制器:
$data=$request->all();
foreach($data['batch_id'] as $batch){
$batch = Batch::find($batch);
$lead = $data['lead_id'];
$batch->lead()->attach($lead,["uuid" => Uuid::generate()]);
$batch_leads = new batch_lead;
$batch_leads->batch_id=$request->input('batch_id');
$batch_leads->lead_id=$request->input('lead_id');
$batch_leads = json_encode($data, true);
型号:
public static function boot()
{
parent::boot();
self::creating(function ($model) {
$model->uuid = (string) Uuid::generate();
});
}
public function getRouteKeyName()
{
return 'uuid';
}