处理未定义的变量

时间:2016-08-20 04:44:25

标签: php laravel

我的索引函数包含变量$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。如果任何变量不存在,则传递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();

    ...
}

4 个答案:

答案 0 :(得分:2)

你所有的变量都有一些东西,我打赌问题在于视野。所以,在视图中做这样的事情:

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

或者,如果要检查定义中的变量是否具有值:

@if (!empty($someVar))

答案 1 :(得分:1)

据我所知,你唯一的问题是$checkforid。只需将其初始化为空数组:

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

一个好的IDE会发出警告并告诉你像#34; $checkforid可能没有定义"。

答案 2 :(得分: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;

答案 3 :(得分:0)

检查变量设置为使用,

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