class Lesson extends Mannequin
{
public operate content material()
{
return $this->morphTo();
}
}
Schema::create('classes', operate (Blueprint $desk) {
$table->id();
$table->unsignedBigInteger('channel_id');
$table->string('title');
$table->nullableMorphs('content material');
$table->timestamps();
$table->international('channel_id')->references('id')->on('channels')->onDelete('cascade');
});
Schema::create('movies', operate (Blueprint $desk) {
$table->id();
$table->string('title');
$table->string('dimension');
$table->string('path');
$table->timestamps();
});
Schema::create('articles', operate (Blueprint $desk) {
$table->id();
$table->string('physique');
$table->string('reading_time');
$table->timestamps();
});
A lesson
can have a Video
or an Article
as a content material, now every little thing works effective, However I’m having a hassle in choosing particular fields whereas keen loading the content material.
Channel::whereId($channelId)->with(['lessons'=> function ($query) {
$query->with('content:id,size');
}])->first();
Since Lesson
content material might be Video
or an Article
an error shall be proven:
Column not discovered: 1054 Unknown column 'dimension' in 'area record' (SQL: choose `id`, `dimension` from `articles` the place `articles`.`id` in (168))
How can I the fields I need whereas keen loading based mostly on the polymorphic relation kind?