T
T
Terroris3372020-12-14 19:29:45
Laravel
Terroris337, 2020-12-14 19:29:45

How to get multiple laravel links?

There is a table of promo codes:

Schema::create('promos', function (Blueprint $table) {
            $table->id();
            $table->string('code')->unique();
            $table->integer('discount')->nullable();
            $table->boolean('delivery_discount')->nullable();
            $table->integer('cur_uses_count')->default(0);
            $table->integer('max_uses_count');
            $table->date('end_date');
            $table->foreignId('product_id')->nullable();

            $table->foreign('product_id')->references('id')->on('products');
            $table->timestamps();
        });


A product is linked to the promo code, it has several links, for example, the product is meat:

Schema::create('products', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->id();
            $table->string('name')->unique();
            $table->string('desc')->nullable();
            $table->string('image');
            $table->integer('price')->unsigned()->nullable();
            $table->integer('proteins')->unsigned()->nullable();
            $table->integer('fats')->unsigned()->nullable();
            $table->integer('energy')->unsigned()->nullable();
            $table->integer('carbo')->unsigned()->nullable();
            $table->boolean('top')->default(0);
            $table->boolean('visibility')->default(1);
            $table->string('mutable_in');
            $table->string('no_mutable_in');
            $table->foreignId('category_id');
            $table->timestamps();

            $table->foreign('category_id')->references('id')->on('categories');
        });


Schema::create('meats', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->integer('price');
            $table->string('category')->default('meat');
            $table->string('desc')->nullable();
            $table->foreignId('product_id');
            $table->timestamps();

            $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
        });


One-to-many relationship (the product has several types of meat, but the meat is associated with only one product).
So far, I have made a request to receive a promo code with the product:

public function promo()
    {
        return $this->hasOne(Promo::class);
    }

public function product()
    {
        return $this->belongsTo(Product::class);
    }


$promo = Promo::with('product')->where('code', 'like', '%' . $request->post('code') . '%')->firstOrFail();


How to get a promo code with a product and all types of product meat?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jazzus, 2020-12-14
@Terroris337

Promo::with('product.meats')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question