2014-07-09 54 views
2

为了避免查询来重复执行一个查询,我改变下面的代码:使用多个操纵

首先块

$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[] = '<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); 

查看:

@if($notifications) 
    @foreach($notifications as $not) 
    {{ $not }} 
    @endforeach 
@endif 

回答

0

方法whereSeen(0)只适用于以前的10个项目,所以看起来这10个项目中没有一个符合该条件这给了count = 0。

$查询 - >获得()执行,$查询已经当执行 - >计数()被调用。

+0

第一个区块$ count和第二个区块$ count有什么区别? – revo

2

让我们先从这一点:

// 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(); 

然后:

$notifications = $user->notifications()->take(10); // Once query runs here 

没有,没有。您的查询在这里执行(与count()):

$count = $user->notifications()->whereSeen(0)->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),它不存在于第1个get查询中。

但是,在这些count中不能有任何区别,除非您查询其他用户。

+0

嗯但我使用'$通知'来显示最后10个项目,其中可能有'看到= 1'列的行是存在的。 – revo

+0

我不明白你的意思。你运行'$ query-> count(); $查询 - >的get()'。这是同一个查询生成器实例。 –

+0

我的意思是它实际上在'take(10)'上执行。正如@marcanuy回答的那样,“whereSeen(0)'仅适用于前10项。 – revo