jeudi 27 janvier 2022

Get the value from 2 table on laravel

Schema::create('discounts', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->dateTime('from', $precision =0);
            $table->dateTime('to', $precision =0);
            $table->enum('status', [0, 1, 2])->default(0)->comment('0:active 1:expired 2:scheduled');
            $table->timestamps();
            $table->softDeletes();
        });

Schema::create('discount_product', function (Blueprint $table) {
            $table->id();
            $table->string('code');
            $table->unsignedBigInteger('discount_id');
            $table->unsignedBigInteger('product_id');
            $table->foreign('discount_id')->references('id')->on('discounts')->onDelete('cascade');
            $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
            $table->float('rate');
            $table->timestamps();
            $table->softDeletes();
        });


Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('name')->unique();
            $table->float('price');
            $table->unsignedBigInteger('user_id');
            $table->unsignedBigInteger('category_id');
            $table->unsignedBigInteger('brand_id');
            $table->string('image')->nullable();
            $table->text('description')->nullable();
            $table->integer('quantity');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
            $table->foreign('brand_id')->references('id')->on('brands')->onDelete('cascade');
            $table->timestamps();
            $table->softDeletes();
        });

public function discountProducts()
    {
        return $this->hasMany(DiscountProduct::class);
    }


 public function discount()
    {
        return $this->belongsTo(Discount::class);
    }

    /**
     * Get product of discount
     */
    public function product()
    {
        return $this->hasMany(Product::class);
    }

I have 3 tables like this : discount, discount_product, product, in my detailProduct.blade.php, I want to get the product have the discount, but I don't know to do that. Can someone help me ? Thanks you very much

This is my : view detail function : I get the discount_products

 public function show($id)
    {
        $discount = $this->discountRepository->getDiscountById($id);
        $discount_products = $this->discountRepository->getDiscontProductOnDiscount($id);

        return view('user.discounts.detailDiscount', 
                   ['discount_products' => $discount_products, 'discount' => $discount]);
    }


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3o3YtL5
via IFTTT

Aucun commentaire:

Enregistrer un commentaire