doutou19761022
2017-01-18 14:27
浏览 995
已采纳

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

I have a one to many relationship between notification and alerFrequencies table. they have models for both. I wrote this function to extract all the latest created_at time stamp from the alerFrequencies table.

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. 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.

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. the notification_id is a foreign key in the alertFreqency table. notification has columns: is, website url and alertFrequencies has : id. notification_id and created_at. now i want to get the latest created_at for every website in the notification table.

图片转代码服务由CSDN问答提供 功能建议

我在notification和alerFrequencies表之间有一对多的关系。 他们有两种模式。 我编写了这个函数来从alerFrequencies表中提取所有最新的 created_at 时间戳。

示例,如果我的通知表中有一个网站,并且警报表中包含 created_at 时间戳,则应该只返回最新的时间戳。 如果我在通知表中有2个具有不同时间戳的网站,它应该返回2个网站的时间戳。

此处只返回最新信息,无论通知表中的网站数量是多少。 如果你有更好的想法来编写查询,我会很感激各种帮助和建议。

  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; 
} 
返回$ alert_timestamp; 
} 
    
 
 

在数据库中我必须使用表通知和alerFrequencies表,具有一对多的关系。 notification_id是alertFreqency表中的外键。 通知有列:是,网站网址和alertFrequencies有:id。 notification_id和created_at。 现在我想获取通知表中每个网站的最新created_at。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dqpu4988 2017-01-18 17:55
    已采纳

    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'). The second thing is that you are selecting just one column. Selecting just one column makes the with function useless. Third thing you are doing wrong is grouping by the created_at column. This simply does nothing for your needs. You would have to 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.

    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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题