Php Laravel 5雄辩地从相关表格中获取记录
我是laravel的新手,我正在使用laravel 5.2。我想在特定请求的预订栏中记录最长结束日期。我的桌子是这样的:Php Laravel 5雄辩地从相关表格中获取记录,php,laravel-5,Php,Laravel 5,我是laravel的新手,我正在使用laravel 5.2。我想在特定请求的预订栏中记录最长结束日期。我的桌子是这样的: request ----------- id | name ---------- 1 | My request bookings ----------- id | request_id | start_date | end_date -------------------------------------------- 1 | 1 | 2016-0
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' );
}
我想拥有最长结束日期的请求数据(例如,上面的记录将是预订Id:3)。请帮忙
我尝试了以下方法:
$request = \DB::table( 'requests' )
->join( 'bookings', 'requests.id', '=', 'bookings.request_id' )
->where( 'requests.id', 1 )
->max( 'bookings.end_date' );
但我想知道如何用雄辩做到这一点。您可以在该日期前描述订单并选择第一张记录:
$request=request::find(1)->bookings()->orderBy('end_date','DESC')->first()代码>表中的预订可以是任意顺序。例如:1 | 1 | 2016-03-01 | 2016-03-05 2 | 1 | 2016-03-25 | 2016-03-25 3 | 1 | 2016-03-10 | 2016-03-20
@Pranab我不明白这个问题,如果你在结束日期之前设置了一个订单
条款,那么无论他们的入境顺序如何,他们都会被订购。上述功能是否适用于您?啊……是的,它正在工作。对不起,我没有第一眼就得到你的答案,但现在清楚了吗。谢谢:)