Support the ongoing development of →
Database Blade
Last updated 2 years ago.

It is very possible but if you are going to use inline SQL throughout your app, you are missing out on some of the key elements of Laravel - especially Eloquent. I recommend giving this page a good read:

If you already have models defined, you are off to a good start. In a nutshell, you can use the following to create the model relationship you need

In your Customer model:

public function orders() {
    return $this->hasMany('orders');

In your Order model:

public function customer() {
    return $this->hasOne('customers');

Now you can utilize Laravel eager loading among numerous other advantages, but still - definitely become more familiar with Eloquent.

But with the code up to this point, your view could do this in your controller action:

$customers = Customer::with('orders')->get();

and this in your view:

@foreach($customers as $customer)
    <td colspan="2">{{ $customer->name }}</td>
    @foreach($customer->orders() as $order)
        <td>{{ $order->description }}</td>

Please take my code with a grain of salt - I didn't check to make sure it works, but it goes to show that you are making your situation much harder than it needs to be, there are a 1001 different ways you can go about your problem, and I'm just showing you one of them.

Last updated 9 years ago.

Thanks DonaldRecord for your answer, this is a one possibility with Eloquent, but i liked understand why not work my sql query directly if someone else could help one understand my mistake, I would appreciate.



Sign in to participate in this thread!


Your banner here too?


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 - All rights reserved.