PHP: 7,3
Laravel: 5,8
Разведчик Ларавела: 7,1
Разведчик Алголии расширен: 1.6
class Page extends Model
{
use Searchable;
public function toSearchableArray()
{
$array = [
'title' => optional($this->content)->title,
'extra' => $this->extra,
];
return $array;
}
public function content()
{
return $this->morphOne(Content::class, 'contentable');
}
protected $fillable = ['extra'];
protected $with = ['content'];
}
class Content extends Model
{
public function contentable()
{
return $this->morphTo();
}
protected $fillable = ['title'];
protected $touches = ['contentable'];
}
class PageController extends Controller
{
public function store(Request $request)
{
$page = Page::create($request->all());
$page->content()->create($request->all());
return $page;
}
public function update(Request $request, $id)
{
$page = $this->findOrFail($id);
$page->update($request->all());
$page->content->update($request->all());
$page = $page->fresh();
return $page;
}
}
Скаутский импорт существующих экземпляров работает нормально, все экземпляры синхронизированы.
При обновлении синхронизация работает нормально, $ touch работает.
$array = [
'title' => 'Title',
'extra' => 'Extra field',
];
При хранении отношение содержимого является нулевым и не обновляется при создании.
Похоже, он не прослушивает содержимое сохраненное событие. (Отношение добавляется в массив $ touch ).
$array = [
'title' => null,
'extra' => 'Extra field',
];