staudenmeir / eloquent-has-many-deep

Laravel Eloquent HasManyThrough relationships with unlimited levels
MIT License
2.67k stars 157 forks source link

Using hasManyThrough and morphToMany #134

Closed d0m4g0j0r closed 3 years ago

d0m4g0j0r commented 3 years ago

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?

staudenmeir commented 3 years ago

Please share the structure of all tables in this chain.

d0m4g0j0r commented 3 years ago
# --- 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');
    });
}
staudenmeir commented 3 years ago

What models/tables can additionalable_type and additionalable_id link to?

d0m4g0j0r commented 3 years ago

For now only additionals and artical_options table

staudenmeir commented 3 years ago

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']
        );
    }
}
d0m4g0j0r commented 3 years ago

It is working, thanks.

But didn't understand anything about that.

staudenmeir commented 3 years ago

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