Closed d0m4g0j0r closed 3 years ago
Please share the structure of all tables in this chain.
# --- restaurants table ---
public function up()
{
Schema::create('restaurants', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->timestamps();
});
}
# --- categories table ---
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->unsignedBigInteger('restaurant_id');
$table->foreign('restaurant_id')->references('id')->on('restaurant');
$table->timestamps();
});
}
# --- articals table ---
public function up()
{
Schema::create('articals', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->unsignedBigInteger('category_id');
$table->foreign('category_id')->references('id')->on('categories');
$table->timestamps();
});
}
# --- artical_options table ---
public function up()
{
Schema::create('artical_options', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->unsignedBigInteger('artical_id');
$table->foreign('artical_id')->references('id')->on('articals');
$table->timestamps();
});
}
# --- additionals table --- (using morphToMany)
public function up()
{
Schema::create('additionals ', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->timestamps();
});
}
# --- addditionalables table ---
public function up()
{
Schema::create('additionalables', function (Blueprint $table) {
$table->unsignedBigInteger('additional_id');
$table->unsignedBigInteger('additionalable_id');
$table->string('additionalable_type');
});
}
What models/tables can additionalable_type
and additionalable_id
link to?
For now only additionals and artical_options table
Use this relationship:
class Restaurant extends Model
{
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
public function additionals()
{
return $this->hasManyDeep(
Additional::class,
[Category::class, Artical::class, ArticalOption::class, 'additionalables'],
[null, null, null, ['additionalable_type', 'additionalable_id'], 'id'],
[null, null, null, null, 'additional_id']
);
}
}
It is working, thanks.
But didn't understand anything about that.
It's a chain of three HasMany
relationships and one MorphToMany
relationship:
https://github.com/staudenmeir/eloquent-has-many-deep#hasmany
https://github.com/staudenmeir/eloquent-has-many-deep#morphtomany
Hi, please can you help me?
My database looks like this:
restaurants hasMany categories categories hasMany articals articals hasMany artical_options artical_options morphToMany additionals
Now I use $restaurant = Restaurant::find($id);
How to get all additionals that are part of selected restaurant?