I have this index function with variables `$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]);
    }

```

if any of these variables doesnot exist, 

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

```

there comes an error undefined variable and whole index page is affected. so i want to skip passing the variable which donot exist. what is the best solution for this?

till now, i have initialized all variables to null.so if any variable doesnot exist null is passed. is it a good practise?

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

    ...
}

```