2015-07-28 6 views
0

Я пытаюсь получить результаты из союза, показывая только результаты, которые были обновлены после того, как я в последний раз проверил (определяется в $ last_check)Сравнение временных меток из двух разных таблиц в Laravel 5

$last_check = CheckNotifications::where('notif_check_user_id', '=', $user_id)->pluck('updated_at'); 

$get_projects = DB::table('requests') 
    ->join('notifications', 'notifications.notif_project_id', '=', 'requests.id') 
    ->select(DB::raw('requests.updated_at, requests.request_name, "" as fullname, "P" as flag')) 
    ->where('notif_user_id', '=', $user_id) 
    ->whereRaw('requests.updated_at > requests.created_at'); 

$comments = DB::table('project_comments') 
    ->leftJoin('users', 'comment_user_id', '=', 'users.id') 
    ->leftJoin('requests', 'comment_project_id', '=', 'requests.id') 
    ->select(DB::raw('project_comments.updated_at, requests.request_name, users.fullname, "C" as flag')); 

$get_notifications = $get_projects->union($comments) 
    ->whereRaw('updated_at > $last_check') 
    ->orderBy('updated_at', 'desc') 
    ->get(); 

Союзные работает нормально пока я не добавлю этот последний оператор whereRaw. С добавлением оператора я получаю ошибку синтаксиса SQL/Access (# 42000). Я думаю, что у него есть что-то, что можно было бы с помощью plucking update_at для сравнения. Есть идеи? Заранее спасибо!

Edit: Вот код ошибки

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '15:09:06) union (select project_comments.updated_at, requests.id as request_id, ' at line 1 (SQL: (select requests.updated_at, requests.id as request_id, requests.request_name, "" as fullname, "P" as flag from `requests` inner join `notifications` on `notifications`.`notif_project_id` = `requests`.`id` where `notif_user_id` = 1 and requests.updated_at > requests.created_at and requests.updated_at > 2015-07-29 15:09:06) union (select project_comments.updated_at, requests.id as request_id, requests.request_name, users.fullname, "C" as flag from `project_comments` left join `users` on `comment_user_id` = `users`.`id` left join `requests` on `comment_project_id` = `requests`.`id`) order by `updated_at` desc) 
+0

Вам может понадобиться сделать '-> whereRaw («updated_at> $ last_check»)', потому что нужно двойные кавычки, чтобы получить $ last_check быть расширенной – RiggsFolly

+0

@RiggsFolly Неа, просто попытался, что и это не сработало , Все еще получите ошибку. – Vikram

+1

положил whereRaw предложение для заказа – user4621032

ответ

0

там whould быть 2 updated_at колонка, Приложите ошибку упоминая что-то подобное еще раз, я не могу быть уверен, что причины.

решение просто написать имя таблицы перед именем столбца Смените

->whereRaw('updated_at > $last_check') 

в

->whereRaw('requests.updated_at > $last_check') 

и изменить orederby(), чтобы быть после whereRaw() вы написали правильный код всего несколько строк: D Надеюсь, что это решит вашу проблему.

+0

Спасибо за предложение, но я все еще сталкиваюсь с ошибкой. Я редактировал исходный вопрос с текстом ошибки. – Vikram

0

добавить, что whereR пункт ав для каждого из двух предыдущих запросов

->where('requests.updated_at','>', $last_check) 

или

->whereRaw('requests.updated_at > ?', [$last_check]) 

вероятно, где не доступен с союзом

просто любопытно: если это работает?

$get_notifications = $get_projects->union($comments) 
    ->where(function($query) use ($last_checked){ 
     $query ->where('requests.updated_at','>', $last_check); 
    }) 
    ->orderBy('requests.updated_at', 'desc') 
    ->get(); 
Смежные вопросы