Чтобы избежать повторного выполнения запроса, я изменил приведенный ниже код:
Первый блок
$user = Auth::user();
$user = User::find($user->id);
$notifications = $user->notifications()->take(10); // Once query runs here
$count = $user->notifications()->whereSeen(0)->count(); // there's a call for a second execution here
$total = $notifications->orderBy('created_at', 'desc')->get();
На это:
Второй блок
$user = Auth::user();
$user = User::find($user->id);
$query = $user->notifications()->orderBy('created_at', 'desc');
$notifications = $query->take(10);
$count = $query->whereSeen(0)->count();
$total = $query->get();
Ну, первый вывод выводится правильно, но во втором $count всегда возвращает int(0), а $total — нет. содержать что-либо. Что не так?
Обновить
начать \global.php:
$user = Auth::user();
$user = User::find($user->id);
$notifications = $user->notifications()->take(10); // Once query runs here
$count = $user->notifications()->whereSeen(0)->count(); // there's a call for a second execution here
$total = $notifications->orderBy('created_at', 'desc')->get();
if($notifications)
{
$msg = array(
'comment' => 'A comment was posted.',
.
.
.
);
$nots = array();
$new = $total->each(function($not) use ($msg, &$nots)
{
$text = $msg[$not->type];
$link = url('dashboard/project/view/'.$not->project_id);
if(!in_array($not->type, array('suggest', 'comment', 'ok', 'notok', 'confirm', 'pre')))
{
$text = str_replace(":nick", $not->project->user->nick, $text);
}
$nots[] = ''.$text.'created_at)).'">';
});
}
.
.
.
View::share('notifications', $nots);
Просмотр:
@if($notifications)
@foreach($notifications as $not)
{{ $not }}
@endforeach
@endif
Подробнее здесь: https://stackoverflow.com/questions/246 ... ipulations