[英]Updating has many relationship in Laravel
I'm building a small application in Laravel 5.5
where I'm having two models: Interaction
and Interaction Summary
in my Interaction
model I'm having following relationship: 我建立一个小应用程序Laravel 5.5
,其中我有两个型号: Interaction
和Interaction Summary
在我的Interaction
我有以下关系模型:
public function meetingSummaries()
{
return $this->hasMany('App\InteractionSummary');
}
Now to create the interaction I'm using following in my controller: 现在要创建交互,请在控制器中使用以下代码:
$data = $request->only('user_id', 'event_type', 'schedule', 'summaries', 'type', 'venue', 'with_client');
$data['schedule'] = Carbon::parse($request->schedule)->addHours(5)->addMinutes(30)->toDateTimeString();
$meeting = [];
$meeting['user_id']= $data['user_id'];
$meeting['schedule'] = $data['schedule'];
$meeting['type'] = $data['type'];
$meeting['with_client'] = $data['with_client'];
$meeting['venue'] = $data['venue'];
$meeting['event_type'] = $data['event_type'];
$interaction = Interaction::create($meeting);
if($data['summaries'])
{
$container = [];
foreach($data['summaries'] as $summary)
{
$summary = (Object)$summary;
if($summary->client['label'])
{
$container[] = new InteractionSummary([
'company_id' => $summary->client['label'],
'nature' => $summary->type,
'user_id' => $summary->mention['label'],
'action' => $summary->action,
'feedback' => $summary->comment
]);
}
}
}
$interaction->meetingSummaries()->saveMany($container);
}
But while updating I don't know to overcome this, as in my fields there might be new or old relational data. 但是在更新时,我不知道该如何克服,因为在我的领域中可能会有新的或旧的关系数据。 I'm trying something like this: 我正在尝试这样的事情:
if($data['summaries'])
{
$container = [];
foreach($data['summaries'] as $summary)
{
$summary = (Object)$summary;
if($summary->id)
{
$container[] = new InteractionSummary([
'id' => $summary->id,
'company_id' => $summary->client['value'],
'nature' => $summary->type,
'user_id' => $summary->mention['value'],
'action' => $summary->action,
'feedback' => $summary->comment
]);
}
else {
$container[] = new InteractionSummary([
'company_id' => $summary->client['value'],
'nature' => $summary->type,
'user_id' => $summary->mention['value'],
'action' => $summary->action,
'feedback' => $summary->comment
]);
}
}
$interaction->meetingSummaries()->save($container);
}
Which is sending me error: 哪个发送给我错误:
Type error: Argument 1 passed to Illuminate\\Database\\Eloquent\\Relations\\HasOneOrMany::save() must be an instance of Illuminate\\Database\\Eloquent\\Model, array given, called in ~\\app\\Http\\Controllers\\InteractionsController.php on line 449 类型错误:传递给Illuminate \\ Database \\ Eloquent \\ Relations \\ HasOneOrMany :: save()的参数1必须是Illuminate \\ Database \\ Eloquent \\ Model的实例,给定数组,在〜\\ app \\ Http \\ Controllers \\ InteractionsController.php中调用在第449行
If I do: 如果我做:
$interaction->meetingSummaries()->saveMany($container);
I get repeated new fields. 我得到重复的新领域。
Guide me how to overcome this. 指导我如何克服这一点。 Thanks. 谢谢。
Use the findOrNew()
method: 使用findOrNew()
方法:
$summaryItem = InteractionSummary::findOrNew($summary->id);
$summaryItem->fill([
'company_id' => $summary->client['value'],
'nature' => $summary->type,
'user_id' => $summary->mention['value'],
'action' => $summary->action,
'feedback' => $summary->comment
]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.