1

I have 3 table Orders , Orderdetails and products table every order is unique and every order have multiple orderdetail with order id and every orderdetail has product_id

I am using a foreach loop in blade

 @foreach($order->orderdetail as $odetail)
  <tr>
    <td><input type="number" value="{{ $odetail->amount }}" ></td>
   </tr>
    @endforeach

this value="{{ $odetail->amount }}" gets value 25 in input field

i have another foreach loop

 @foreach($products as $key => $p)
  <tr>
    <td><input type="number" value="{{ $p->order->orderdetail->unit }}"></td>
   </tr>
    @endforeach

i want that above loop value="{{ $odetail->amount }}" output in 2nd foreach loop so i try this

value="{{ $p->order->orderdetail->unit }}" 

if orderdetail gets value in first loop then why it is not getting value in 2nd foreach loop can someone clear me what is mistake and how doeas model works and how i can get value in 2nd loop?

here is my Order model class and orderdetail function

class Order extends Model
{
    public function orderdetail(){
            return $this->hasMany('App\OrderDetail','order_id');
        }
}

and here is my orderdetail class and functions

class OrderDetail extends Model
{    
    public function order(){
        return $this->belongsTo('App\Order');
    }

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

here it is product model

class Product extends Model
{
    public function category(){
        return $this->belongsTo('App\Category');
    }
}

databse image of orderdetai enter image description here

8
  • Can you share your model's please, including the "unit" field. But a broad assumption would be that your unit field is also a relational field, hence it is not possible to output the collection directly. Commented Jan 9, 2021 at 12:39
  • please show model query form which you are fetching data Commented Jan 9, 2021 at 12:48
  • type dd($products) and see the records over there Commented Jan 9, 2021 at 12:48
  • Can you share the product model as well? Commented Jan 9, 2021 at 12:55
  • thanx for every contributors i have updated code with all the models which are used Commented Jan 9, 2021 at 12:57

1 Answer 1

1

It seems that you just missed the order relation in the Product model

public function orders(){ // by laravel convention you need a plural name for hasMany relation
    return $this->hasMany('App\order','product_id');
}

I hope it will solve your problem.

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

2 Comments

in order table i did not use order_id i only have id so when i pass hasMany('App\Order','id'); it gives me error [orderdetail] does not exist
My bad. I have changed the answer. There should be the product_id instead of order_id

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.