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

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: http://laravel.com/docs/4.2/eloquent.

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:

<table>
<thead>
<tr>
    <th>Customer</th>
    <th>Orders</th>
</tr>
</thead>
<tbody>
@foreach($customers as $customer)
<tr>
    <td colspan="2">{{ $customer->name }}</td>
</tr>
    @foreach($customer->orders() as $order)
    <tr>
        <td></td>
        <td>{{ $order->description }}</td>
    </tr>
    @endforeach
@endforeach
</tbody>
</table>

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.
0

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.

Regards!

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

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

Your logo here?

Laravel.io

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

© 2024 Laravel.io - All rights reserved.