简体   繁体   English

Laravel:一对多关系只返回一条记录

[英]Laravel: one to many relationship returns only one record

I have a one to many relationship between notification and alerFrequencies table. 我在通知和alerFrequencies表之间有一对多的关系。 they have models for both. 他们有两种模式。 I wrote this function to extract all the latest created_at time stamp from the alerFrequencies table. 我写了这个函数来从alerFrequencies表中提取所有最新的created_at时间戳。

Example, if I have one website in my notification table with created_at time stamps in the alert table, it should return me the latest time stamp only. 例如,如果我的通知表中有一个网站,并且警报表中包含created_at时间戳,则它应该仅返回最新的时间戳。 If I have 2 websites in the notification table with a different time stamp, it should return the time stamp for the 2 websites apart. 如果我在通知表中有2个具有不同时间戳的网站,它应该返回2个网站的时间戳。

Here it returns only the latest regardless of the number of websites in the notification table. 无论通知表中的网站数量如何,它都只返回最新的。 ony one who get a better idea to write the query, i would appreciate all kinds of help and suggestions. 一个谁有更好的想法来编写查询,我会感谢各种帮助和建议。

public function alert(){
        $alert_timestamp = AlertFrequency::with('notification')->select('created_at')->groupBy('created_at')->orderBy('created_at','DESC')->first();
        $alert_timestamp=$alert_timestamp->created_at->toDateTimeString();
        if($alert_timestamp==null){
            return false;
        }       
            return $alert_timestamp;
    }

in the database i have to tables notifications and alerFrequencies table, with one to many relationship. 在数据库中我必须有表通知和alerFrequencies表,具有一对多的关系。 the notification_id is a foreign key in the alertFreqency table. notification_id是alertFreqency表中的外键。 notification has columns: is, website url and alertFrequencies has : id. 通知有列:是,网站网址和alertFrequencies有:id。 notification_id and created_at. notification_id和created_at。 now i want to get the latest created_at for every website in the notification table. 现在我想获取通知表中每个网站的最新created_at。

There are a few things that you are doing wrong. 有些事情你做错了。 First you are querying AlertFrequency::with('notification') while from what I am understanding you might want something like Notification::with('alertfrequencies') . 首先,您正在查询AlertFrequency::with('notification')而根据我的理解,您可能需要Notification::with('alertfrequencies') The second thing is that you are selecting just one column. 第二件事是你只选择一列。 Selecting just one column makes the with function useless. 只选择一列会使with函数无效。 Third thing you are doing wrong is grouping by the created_at column. 你做错的第三件事是按created_at列进行分组。 This simply does nothing for your needs. 这根本不能满足您的需求。 You would have to groupBy('notification_id') . 你必须要groupBy('notification_id') I wrote the above explanations so you can understand better the logic of your application, while I am giving a possible solution below. 我写了上面的解释,这样你就可以更好地理解你的应用程序的逻辑,而我在下面给出了一个可能的解决方案。

Given that you want the latest timestamp of a notification for each website, then a possible approach would be to add an accessor in you Notification model. 鉴于您需要每个网站的通知的最新时间戳,那么可能的方法是在您的Notification模型中添加访问者。

public function getLastTimestampAttribute(){
    return AlertFrequency::where('notification_id', $this->attributes['id'])->order_by('created_at','desc')->first()->created_at;
}

Then you can easily access the latest timestamp for each notification by doing $notification->last_timestamp , supposing that $notification is your object on the view. 然后,您可以通过执行$notification->last_timestamp轻松访问每个通知的最新时间戳,假设$notification是您在视图上的对象。

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

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