Laravel,如何编写此查询以显示包含来自不同表的字段的记录以及如何更新它们?

时间:2014-10-07 20:40:50

标签: mysql sql laravel eloquent

我正在考虑实现类似http://www.databaseanswers.org/data_models/event_reservations/index.htm

的架构设计

我需要显示考勤表,其中显示了活动的客户名称和席位。如何编写查询/查询,以便我可以轻松显示数据并在必要时进行更新?

1 个答案:

答案 0 :(得分:0)

如果您使用Eloquent且模型设置正确,则查询示例如下:

$reservations = CustomerBooking::where('event_id', $id)
    ->with('event', 'customer', 'seat')->get();

在视图中:

Event Name: {{ $reservations->event->event_name->first() }} at {{ $reservations->event->venue->venue_name->first() }}
{{ $reservations->event->event_start_date->first() }} to {{ $reservations->event->event_end_date->first() }}

客户资料:

@foreach($reservations as $reservation)
Name: {{ $reservation->customer->customer_name }}
Seat: {{ $reservation->seat->seat_number }}
@endforeach