micky
micky

Reputation: 317

dealing with undefined variable

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

    ...
}

Upvotes: 4

Views: 379

Answers (4)

vfsoraki
vfsoraki

Reputation: 2307

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;

Upvotes: 1

Paul Spiegel
Paul Spiegel

Reputation: 31792

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".

Upvotes: 1

LF-DevJourney
LF-DevJourney

Reputation: 28529

check variable is set use,

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

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163768

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

Upvotes: 2

Related Questions