Использование одного запроса для нескольких манипуляцийPhp

Кемеровские программисты php общаются здесь
Ответить
Anonymous
 Использование одного запроса для нескольких манипуляций

Сообщение Anonymous »

Чтобы избежать повторного выполнения запроса, я изменил приведенный ниже код:

Первый блок
$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
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Php»