0

I have 2 tables, product and promotion. I do not know how I can display the price of both product and promotion(price after discount) based on product_id in blade. In blade i can only display price for promotion using {{$promotion->price}}

Product Table

id - name - price

Promotion Table

id product_id - price - discount

Controller:

 $latestProduct = Promotion::orderBy('created_at', 'desc')
    ->with('product')
    ->take(20)
    ->get();

Model:

  1. Product Model

    public function promotion()
    {
       return $this->hasMany('App\Model\Promotion');
    }
    
  2. Promotion Model

    public function product()
    {
        return $this->belongsTo('App\Model\Product');
    }
    

2 Answers 2

2

in blade file:

Edit:

@foreach($latestProduct as $product)
    {{$product->price}}
    {{$product->promotion->price}}
@endforeach

would work.

Sign up to request clarification or add additional context in comments.

Comments

0

I think it'll be better to make like this:

$latestProducts = Product::orderBy('created_at', 'desc')
->with('promotion')
->take(20)
->get();

And, as mentioned Ali Özen, in your blade file you'll be able to iterate through your products and get product's promotion price with:

$product->promotion->price

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.