Laravel:mysql在json中获取所有未读和最后15个未读通知

时间:2015-05-22 14:35:05

标签: mysql json laravel

我正在处理应用程序中的Notification系统,我仍然坚持获取所有未读通知+最后15个读取通知我也在html中排序问题它在json中给出了axact结果但是当我在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);
}

1 个答案:

答案 0 :(得分: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();

您可能希望对其进行编辑以满足您的需求。