У меня есть эти связанные таблицы:
[Template] ---- [HasOne]--> [Checklist] ---- [HasMany]--> [Items]
Мне интересно, как я могу обновить эти связанные данные в одном процессе отношений, чтобы это можно было сделать плавно.
Вот моя модель:
Шаблон:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Template extends Model
{
protected $table = 'templates';
protected $fillable = [
'name'
];
public function checklist(){
return $this->hasOne(Checklist::class,'template_id');
}
}
Контрольный список
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Item;
use Carbon\Carbon;
class Checklist extends Model
{
protected $table = 'checklists';
protected $fillable = [
'template_id',
'description',
'due_interval',
'due_unit'
];
public function template(){
return $this->belongsTo(Template::class,'template_id');
}
public function items(){
return $this->hasMany(Item::class,'checklist_id');
}
}
Item
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
class Item extends Model
{
protected $table = 'items';
protected $fillable = [
'description',
'urgency',
'due_interval',
'due_unit'
];
public function checklists(){
return $this->belongsTo(Checklist::class,'checklist_id');
}
}
Это мой TemplateController:
public function update(Request $request,$templateId)
{
$req = json_decode($request->getContent(),true);
$data = $req['data'];
$findtemplate = Template::find($templateId)->first();
$exetemplate = $findtemplate->update(
[
'name' => $data['name']
]
);
var_dump('Update Template: ',$exetemplate);
$exechecklist = $findtemplate->checklist()->update(
[
'description' => $data['checklist']['description'],
'due_interval' => $data['checklist']['due_interval'],
'due_unit' => $data['checklist']['due_unit']
]
);
var_dump('Update Checklist: ',$exechecklist);
$execitem = $findtemplate->checklist()->items()->update(
[
'description' => $data['items']['description'],
'urgency' => $data['items']['urgency'],
'due_interval' => $data['items']['due_interval'],
'due_unit' => $data['items']['due_unit']
]
);
var_dump('Create Items: ',$execitem);
}
Выше код показывает эти ошибки:
string (17) "Шаблон обновления:" bool (true) string (18) "Контрольный список обновлений:" int (0) Вызов неопределенного метода Illuminate \ Database \ Eloquent \ Relations \ HasOne ::items () (1/1) BadMethodCallException Вызов неопределенного метода Illuminate \ Database \ Eloquent \ Relations \ HasOne :: items ()
Я хочу обновить эту связанную таблицу с использованием передового опыта.Как это сделать?Пожалуйста, помогите?