Laravel 4 使用一个查询进行多个操作

Laravel 4 使用一个查询进行多个操作,laravel-4,Laravel 4,为了避免重复执行查询,我更改了以下代码: 第一块 $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

为了避免重复执行查询,我更改了以下代码:

第一块

$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();
为此:

第二块

$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
不包含任何内容。怎么了

更新

start\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[] = '<a href="'.$link.'" class="item"'.($not->seen == 0 ? ' style="background-color: #EBF3EF;"' : '').'><i class="icon-signin"></i>'.$text.'<span class="time"><i class="icon-time" title="'.date('m/d', strtotime($not->created_at)).'"></i></span></a>';
        });
    }
    .
    .
    .
    View::share('notifications', $nots);
方法whereSeen(0)仅适用于前10项,因此这10项中似乎没有一项与该条件匹配,这将给出count=0

执行$query->get()时,调用->count()时,已经执行了$query。

让我们从以下内容开始:

// Assuming you use eloquent user provider
$user = Auth::user(); // 1st query for user
$user = User::find($user->id); // 2nd query for user
相反:

$user = Auth::user();
然后:

不,没有。您的查询在此处执行(使用
count()
):


现在,您的第二个代码块执行以下操作:

// $query has orderBy
$query = $user->notifications()->orderBy('created_at', 'desc');

// $query has limit(10)
$notifications = $query->take(10);

// $query has where clause
$count = $query->whereSeen(0)->count();

// $query still has all of the above
$total = $query->get();
因此,如果
count()
返回
0
,那么显然
get()
也将返回空集合

这些代码块的唯一区别是
whereSeen(0)
,它在第一次
get
查询中不存在


但是,除非您查询其他用户,否则这些
计数不会有任何差异。

Hmmm但我使用
$notifications
显示最后10项,其中可能存在一行
seen=1
列。我不明白您的意思。运行
$query->count()$查询->获取()
。这是同一个查询生成器实例。我的意思是它实际上在
take(10)
执行。正如@marcanuy所回答的,
whereSeen(0)
仅适用于前10项。正如我所说,
take
不执行查询。它只对其应用
LIMIT 10
子句。那么我就不能在
$notifications
上使用
foreach
循环来显示最后十个通知了!第一块
$count
和第二块
$count
之间有什么区别?
// $query has orderBy
$query = $user->notifications()->orderBy('created_at', 'desc');

// $query has limit(10)
$notifications = $query->take(10);

// $query has where clause
$count = $query->whereSeen(0)->count();

// $query still has all of the above
$total = $query->get();