2014-07-09 86 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