简体   繁体   English

如何使用 Laravel 使用 where 子句获取表中的最后一条记录?

[英]How do I get the last record in a table with a where clause using Laravel?

I'm creating database notifications.我正在创建数据库通知。 I have 2 Notification classes: InterviewRequestReceived.php:我有 2 个通知类:InterviewRequestReceived.php:

$user = Auth::user();
        $interviewRequestReceived = InterviewRequestsReceived::latest()->where('user_id', $user->id)->get();
        return [
            'date_time1' => $interviewRequestReceived[$interviewRequestReceived->count() - 1]->latest()->get('date_time1'),
            'date_time2' => $interviewRequestReceived[$interviewRequestReceived->count() - 1]->latest()->get('date_time2')
        ];

and InterviewRequestSent.php:和InterviewRequestSent.php:

public function toDatabase($notifiable)
    {
        $user = Auth::user();
        $interviewRequestSent = InterviewRequestsSent::latest()->where('user_id', $user->id)->get();
        return [
            'date_time1' => $interviewRequestSent[$interviewRequestSent->count() - 1]->latest()->get('date_time1'),
            'date_time2' => $interviewRequestSent[$interviewRequestSent->count() - 1]->latest()->get('date_time2')
        ];
    }

I have 3 tables.我有3张桌子。 interview_requests_receiveds, interview_requests_sents and I created the notifications table and migrated. Interview_requests_receiveds、interview_requests_sents 和我创建了通知表并进行了迁移。 On my form I have 2 options for datetime fields, so Employers can choose 2 possible date and times that would work for them to interview a candidate.在我的表单上,我有 2 个日期时间字段选项,因此雇主可以选择 2 个可能的日期和时间来面试候选人。 My form is working.我的表格正在工作。 My notifications are working except it's inserting all of the logged in Users' date and times, instead of just the last inserted record.我的通知正在工作,只是它插入了所有登录用户的日期和时间,而不仅仅是最后插入的记录。

I'm trying to use latest(), but it's not working.我正在尝试使用 latest(),但它不起作用。

As per Laravel Documentation根据 Laravel 文档

The latest and oldest methods allow you to easily order results by date . latestoldest方法使您可以轻松地按日期结果进行排序 By default, result will be ordered by the created_at column.默认情况下,结果将按created_at列排序。 Or, you may pass the column name that you wish to sort by或者,您可以传递您希望排序的列名

If you want to use latest then instead of get() use first() function:如果你想使用最新的而不是get()使用first()函数:

If there is already created_at column in your table then如果您的表中已经有created_at列,那么

$interviewRequestSent = InterviewRequestsSent::latest()->where('user_id', $user->id)->first();

OR或者

Assuming you want to get last record based on id column假设您想根据id列获取最后一条记录

 $interviewRequestSent = InterviewRequestsSent::latest('id')->where('user_id', $user->id)->first();

Or you can get the last record using any below methods或者您可以使用以下任何方法获取最后一条记录

InterviewRequestsSent::where('user_id', $user->id)->last();

OR或者

InterviewRequestsSent::where('user_id', $user->id)->orderBy('id', 'desc')->first();`

Reference:参考:

Laravel -> Query Builder -> latest method Laravel -> 查询生成器 -> 最新方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM