1
//model
return $this->hasMany('App\Model\Order', 'customer_id')->select('name');

//controller
$customer = Model\customer::find($id);

return view('customer', array('data' => $customer));

//view (blade)
{{ $data }} //{"id":1,"name":"Tony"} 
{{ $data->orders }} //[{"name":"T-shirt"},{"name":"Macbook"}]

i'm new in laravel

I have a question about passing data to view.blade

I used hasMany to join 2 table together and pass $data to view

when I try to output $data, I could not see order object inside of data object

but when I did $data->orders, it shows object

can anyone tell me how it works?

3 Answers 3

1

The behavior you are seeing is because Laravel lazy loads relations when they are accessed. If the relation has not been loaded, Laravel will send another query and add it to your $data object behind the scenes. That's why when you dump the $data variable, you are not seeing the orders.

To demonstrate, run the following snippet.

{{ $data }} //{"id":1,"name":"Tony"} 

// Laravel will lazy load the orders relation
{{ $data->orders }} //[{"name":"T-shirt"},{"name":"Macbook"}]

// Now the $data object has the orders property.
{{ $data }} //{"id":1,"name":"Tony", "orders": [{"name":"T-shirt"},{"name":"Macbook"}]}

Solution

You have a couple options here. Here are 2.


1. Eager load relation when querying model.

$customer = Model\customer::with('orders')->find($id);

This is the preferred method as it prevents n+1 querying.

2. Load relation after model has been queried.

$customer = Model\customer::find($id);

$customer->load('orders');

Now when you dump the model, you will see the orders property.

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

5 Comments

do I still keep $this->hasMany('App\Model\Order', 'customer_id')->select('name'); inside of my model?
Yes, keep it. Also, I made an update to the answer explaining the behavior you are seeing.
one more question, should I use hasMany to join? or use db builder to create join? $this->select(...)->leftJoin('orders', function($join){...
hasMany is easier but db builder is more make sense for me
Using Eloquent's given relation methods is much better. You have the advantage of shorter query statements, nested relation loading, among many others. So to me, using hasMany would be the preferred method. So, say your Order model also has a relation called Items, To load both relations of the customer, you would simply run Customer::with('orders.items')->find($id).
0

In your model,

public function orders(){
 return $this->hasMany('App\Model\Order', 'customer_id');
}

In view

{{ $data->orders->order_name }} 

I hope it will works

1 Comment

i understand that, its not what i asking, im asking inside of my blade, i print out $data, why i didn't see orders inside of my object ($data->orders this means orders is part of data)
0

You have defined hasMany relationship in your model. So, for a particular customer there can be multiple orders. So, it shows in array. You have to loop through:

foreach($data->orders as $order)
{
    $order->name
}

to get the orders.

1 Comment

$data->orders this means orders should be inside of data object, when I print out $data, why i couldn't see $orders object inside of $data object

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.