处理未定义的变量

dealing with undefined variable

提问人:micky 提问时间:8/20/2016 更新时间:8/23/2016 访问量:350

问:

我有这个带有变量的索引函数,,,。$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]);

出现错误 undefined 变量,整个索引页都会受到影响。所以我想跳过传递不存在的变量。最好的解决方案是什么?

到目前为止,我已经初始化了所有变量,以 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();

    ...
}
PHP Laravel

评论

0赞 Mittul Chauhan 8/20/2016
可能的重复 stackoverflow.com/questions/17767094/...
0赞 Maninderpreet Singh 8/20/2016
if (isset($someVar)) 检查

答:

2赞 Alexey Mezenin 8/20/2016 #1

你所有的变量都会有一些东西,我敢打赌问题出在视图中。因此,只需在视图中执行如下操作:

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

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

@if (!empty($someVar))

评论

0赞 micky 8/20/2016
我开始知道问题,如果(Auth::check())条件失败。这些变量将未定义。所有其他变量都会有一些东西$check$checkforid
0赞 Winter 8/20/2016
如果问题出在您看来,您可以简化第一个要使用的问题,然后在@forelse($products as $product)@empty
0赞 LF00 8/20/2016 #2

检查变量被设置使用,

$data = array();
if(isset($products))
    $data['products'] = $products;
...
return View('product.index', $data);
1赞 Paul Spiegel 8/21/2016 #3

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

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

一个好的 IDE 会警告并告诉你类似“可能未定义”的内容。$checkforid

评论

0赞 micky 8/21/2016
$check也是可用的。
0赞 Paul Spiegel 8/21/2016
哈哈。$check 在访问时都是一个数组。
1赞 vfsoraki 8/23/2016 #4

也有这个解决方案,在我看来更优雅:

    $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;