【发布时间】:2016-02-09 10:54:51
【问题描述】:
我是 laravel 新手,我正在使用 Laravel 5.2 我想在特定请求的预订列中记录最长结束日期。我的桌子是这样的:
request
-----------
id | name
----------
1 | My request
bookings
-----------
id | request_id | start_date | end_date
--------------------------------------------
1 | 1 | 2016-03-01 | 2016-03-05
2 | 1 | 2016-03-10 | 2016-03-20
3 | 1 | 2016-03-25 | 2016-03-28
Request Model
------------------
public function bookings()
{
return $this->hasMany( 'App\Booking' );
}
我希望获得具有最长结束日期的请求数据(对于上面的示例,记录将是 booking Id : 3 )。请帮忙。
我尝试了以下方法:
$request = \DB::table( 'requests' )
->join( 'bookings', 'requests.id', '=', 'bookings.request_id' )
->where( 'requests.id', 1 )
->max( 'bookings.end_date' );
但我想知道如何用 eloquent 做到这一点。
【问题讨论】: