Hi, I'm trying to make relationships with the tables in Laravel, but despite the fact that I'm understanding them more, I can't do this.
my table tables
go | yam | zone |
---|---|---|
1 | Wear | two |
two | table 1 | two |
two | table 2 | two |
My orders table
go | prepared | ticket | table_id | product |
---|---|---|---|---|
1 | 1 | 2021178 | 1 | Pizza Carbonara |
two | 0 | 2021179 | two | Soda |
two | 0 | 2021179 | two | Coffee |
two | 0 | 2021179 | two | roman pizza |
As you can see, the table orders
has a field called prepared, so the first thing I want to do is get the tables that have an unprepared order, which is = 0. Then, within each table, I want to get the orders that are unprepared but grouped by ticket. , that is, it can print the ticket number as the title, but iterate the products that the ticket has under the title.
To remove the tables that are with an unprepared order, that is to say that they are active, I do the following.
$mesas = Mesas::with('pedidos')->join('orders', 'mesas.id', '=', 'orders.mesa_id')
->select('mesas.mesa', 'mesas.id', 'orders.mesa_id', 'orders.tipo_pedido', 'orders.preparado')
->groupBy('mesas.mesa', 'mesas.id', 'orders.mesa_id', 'orders.tipo_pedido', 'orders.preparado')
->where('orders.preparado', 0)
->where('orders.tipo_pedido', 1)->get();
My model Tables
public function pedidos()
{
return $this->hasMany(Order::class, 'id', 'mesa_id');
}
then in my sight
@foreach ($mesas as $item)
<li>
{{ $item->mesa }}
</li>
@foreach ($item->pedidos as $peds)
<div>{{ $peds->ticket }}</div>
@endforeach
@endforeach
If I get the tables that are with prepared = 0 but I have to group them so that they are not duplicated, but I can't get the ticket once and below that, iterate the products that that ticket has.
Well in the end I got what I want:
First I look for the active tables in an order with the prepared field = 0:
In my Tables model, I pull out all the products associated with that table:
In my sight.