Чтобы избежать повторного выполнения запроса, я изменил приведенный ниже код:
Первый блок
$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
Использование одного запроса для нескольких манипуляций ⇐ Php
Кемеровские программисты php общаются здесь
-
Anonymous
1728770828
Anonymous
Чтобы избежать повторного выполнения запроса, я изменил приведенный ниже код:
[b]Первый блок[/b]
$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();
На это:
[b]Второй блок[/b]
$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 — нет. содержать что-либо. Что не так?
[b]Обновить[/b]
начать \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[] = '[i][/i]'.$text.'[i]created_at)).'">[/i]';
});
}
.
.
.
View::share('notifications', $nots);
Просмотр:
@if($notifications)
@foreach($notifications as $not)
{{ $not }}
@endforeach
@endif
Подробнее здесь: [url]https://stackoverflow.com/questions/24647695/using-one-query-for-multiple-manipulations[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия