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

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.

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn