【问题标题】:Laravel: mysql Get all Unread and 15 last unread notification in jsonLaravel:mysql在json中获取所有未读和最后15个未读通知
【发布时间】:2015-08-04 15:24:38
【问题描述】:

我正在处理应用程序中的通知系统,我被困在获取所有未读通知 + 最后 15 个已读通知我在 html 中也有排序问题,它在 json 中给出了准确的结果,但是当我嵌入 html 时,它会自动排序:(

这是我的 Laravel 代码:

public function ajaxNotificaitons() {
    $customFunctions = new \admin\library\myFunctions;
    $where = array('send_to' => Auth::user()->id);
    $notifications = Notification::where($where)->orderby('id', 'desc')->get();

    // return $notifications;
    $jsonData = array();
    $i = 0;
    foreach ($notifications as $notification) {
        $data = $customFunctions->getUserDetail($notification->send_by);
        $row = array();
        $row['id'] = $notification->id;
        $row['order_id'] = $notification->order_id;
        $row['message'] = $notification->message;
        $row['send_by'] = $data['username'];
        $row['send_to'] = $notification->send_to;
        $row['is_read'] = $notification->is_read;
        $row['is_hidden'] = $notification->is_hidden;
        $row['is_deleted'] = $notification->is_deleted;
        $row['date'] = date('d-m-Y', strtotime($notification->created_at));
        $row['time'] = date('H:i:s', strtotime($notification->created_at));
        $jsonData[$i++] = $row;
    }

    return Response::json($jsonData);
    // return json_encode($jsonData);
}

【问题讨论】:

    标签: mysql json laravel


    【解决方案1】:

    我认为这样的事情应该可行:

        Notification::where('send_to', Auth::user()->id)->where('status', 'unread')->union(Notification::where('send_to', Auth::user()->id)
                                 ->where('status', 'read')
                                 ->orderBy('id', 'desc')
                                 ->limit(15)
                                 ->getQuery()
    )->get();
    

    您可能想要编辑它以满足您的需要。

    【讨论】:

    • NotificationsController.php 第 31 行中的 FatalErrorException:语法错误,意外 ';' :( ->limit(15);
    • @hy_sultani 修复一个简单的语法错误应该完全在你的能力范围内。
    • @hy_sultani 哦,限制之后不应该有分号,你也需要在getQuery() 之后在limit() 之后。请参阅编辑后的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-10
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 2016-11-06
    相关资源
    最近更新 更多