2016-08-20 64 views
4

我有一個變量$product$categories$most_views$show$check$checkforid該指數函數。處理未定義的變量

public function index() 
    { 
     $products=Product::where(['status'=>'1'])->orderBy('most_viewed','desc')->with('category')->get(); 
     $categories=Category::all(); 
     $mostviews=Product::where(['status'=>'On sale'])->orderBy('most_viewed','desc')->limit(10)->get(); 
     $show=Product::orderBy('most_viewed','desc')->with('category') 
                ->with('user') 
                ->with('productbrand.brand')           
                ->first(); 

     if(Auth::check()) 
     { 
      $check=Watchlist::where(['user_id'=>Auth::user()->id])->get()->toArray(); 
      foreach($check as $che) 
      { 
       $checkforid[]=$che['product_id']; 
      } 
     }  

     return View('product.index',['products'=>$products,'mostviews'=>$mostviews,'show'=>$show,'checkforid'=>$checkforid,'categories'=>$categories]); 
    } 

如果這些變量的亙古不變的存在,

return View('product.index',['products'=>$products,'mostviews'=>$mostviews,'show'=>$show,'checkforid'=>$checkforid,'categories'=>$categories]); 

總會有一個錯誤未定義的變量和整個索引頁會受到影響。所以我想跳過不存在的變量。這是最好的解決方案是什麼?

直到現在,我已經初始化所有變量爲null.so,如果任何變量不存在null傳遞。這是一個好習慣嗎?

public function index() 
    { 
     $products=null; 
     $show=null; 
     $check=null; 
     $checkforid=null; 
     $mostviews=null; 
     $categories=null; 

     $products=Product::where(['status'=>'1'])->orderBy('most_viewed','desc')->with('category')->get(); 
     $categories=Category::all(); 
     $mostviews=Product::where(['status'=>'On sale'])->orderBy('most_viewed','desc')->limit(10)->get(); 

    ... 
} 
+0

可能重複http://stackoverflow.com/questions/17767094/check-if-array-value-isset-and- is-null –

+0

if(isset($ someVar))檢查 –

回答

1

據我所知,你唯一的問題是$checkforid。只是初始化爲空數組:

$checkforid = []; 
if(Auth::check()) 
{ 
    ... 
    $checkforid[]= ... 
    ... 
} 

一個好的IDE會發出警告,告訴你什麼「$checkforid可能無法定義」。

+0

也是$ check可用。 – micky

+0

否.. $ check在訪問數據時總是一個數組。 –

2

所有的變量都會有東西,我敢打賭問題是在視圖中。所以,僅僅做這樣的事情在一個觀點:

@if (count($products) > 0) 
    @foreach ($products as $product) 
    .... 
@endif 

或者,如果你想檢查是否定義的變量,並且有一個值:

@if (!empty($someVar)) 
+0

如果if(Auth :: check())條件失敗,我就知道'$ check'和'$ checkforid'的問題。這些變量將是未定義的。所有其他變量將有一些東西 – micky

+0

如果問題出現在您的視圖中,您可以簡化第一個使用'@forelse($ products as $ product)',然後在'@ empty'下處理null – Winter

0

檢查參數設置使用,

$data = array(); 
if(isset($products)) 
    $data['products'] = $products; 
... 
return View('product.index', $data); 
+0

函數isset not is_set ... – Jaimin

+0

抱歉,我現在糾正它。 –

1

有這種解決方案也是如此,這在我看來是更優雅:

$products=Product::where(['status'=>'1'])->orderBy('most_viewed','desc')->with('category')->get(); 
    $categories=Category::all(); 
    $mostviews=Product::where(['status'=>'On sale'])->orderBy('most_viewed','desc')->limit(10)->get(); 
    $show=Product::orderBy('most_viewed','desc')->with('category') 
               ->with('user') 
               ->with('productbrand.brand')           
               ->first(); 
    $view = View('product.index',['products'=>$products,'mostviews'=>$mostviews,'show'=>$show,'categories'=>$categories]); 
    if(Auth::check()) 
    { 
     $check=Watchlist::where(['user_id'=>Auth::user()->id])->get()->toArray(); 
     foreach($check as $che) 
     { 
      $checkforid[]=$che['product_id']; 
     } 
     $view->with('checkforid', $checkforid); 
    }  

    return $view;