4

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

    ...
}
micky
  • 277
  • 1
  • 13
  • 39

4 Answers4

2

All your variables will have something and I bet problem is in a view. So, just do something like this in a view:

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

Or if you want to check if variable in defined and has a value:

@if (!empty($someVar))
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • i came to know the problem with `$check` and `$checkforid` if if(Auth::check()) condition fails. these variables will be undefined. all other variables will have something – micky Aug 20 '16 at 05:04
  • If the problem is in your view, you can simplify the first one to use `@forelse($products as $product)` then deal with null under `@empty` – Winter Aug 20 '16 at 05:13
1

As far as i see, your only problem is $checkforid. Just initialize it as an empty array:

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

A good IDE would warn and tell you something like "$checkforid might not be defined".

Paul Spiegel
  • 30,925
  • 5
  • 44
  • 53
1

There is this solution too, which is more elegant in my opinion:

    $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;
vfsoraki
  • 2,186
  • 1
  • 20
  • 45
0

check variable is set use,

$data = array();
if(isset($products))
    $data['products'] = $products;
...
return View('product.index', $data);
LF00
  • 27,015
  • 29
  • 156
  • 295