我想在视图中访问查询中的值。
$bookings = DB::table('bookings')
->join('staffs', 'staffs.id' , '=', 'bookings.staff_id')
->join('customers', 'customers.id' , '=', 'bookings.customer_id')
->select('bookings.id', 'bookings.start_time', 'bookings.end_time', 'bookings.service', 'staffs.name as Staff-Name', 'customers.name as Customer-Name')
->orderBy('customers.name', 'desc')
->get();
return view('booking.index')->with('bookings', $bookings);
这很容易理解。我想要做的是,我想获得start_time,end_time在预订表中,名称是员工表也在客户表中名称。
现在我正在做:
@foreach($bookings as $booking)
{{ $booking->start_time }}
{{ $booking->end_time }}
{{ $booking->name }} // name column which is in customers table
@endforeach
但这些事似乎不起作用。
答案 0 :(得分:2)
这都是因为您使用的是列名而不是您设置的别名。
看看它,例如customers.name被命名为客户名称。
->select('bookings.id', 'bookings.start_time', 'bookings.end_time', 'bookings.service', 'staffs.name as Staff-Name', 'customers.name as Customer-Name')
如果您想访问数据,您需要这样做:
@foreach($bookings as $booking)
{{ $booking->start_time }}
{{ $booking->end_time }}
{{ $booking->Customer-Name }} // name of alias
@endforeach
无论如何我不认为别名中的破折号是好的解决方案,你应该使用下划线。 (customers.name as Customer_Name)
和$booking->Customer_Name
答案 1 :(得分:0)
试试这个
$bookings = DB::table('bookings')
->join('staffs', 'staffs.id' , '=', 'bookings.staff_id')
->join('customers', 'customers.id' , '=', 'bookings.customer_id')
->select('bookings.id', 'bookings.start_time', 'bookings.end_time', 'bookings.service', 'staffs.name as Staff-Name', 'customers.name as Customer-Name')
->orderBy('customers.name', 'desc')
->get();
return view('booking.index')->with(compact('bookings'));
答案 2 :(得分:0)
$ bookings包含一系列数据。您可以在视图中打印这些..
@foreach($bookings as $booking)
{{$booking->start}}
{{$booking->end_time}}
@endforeach