php - dealing with undefined variable
Get the solution ↓↓↓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();
...
}
Answer
Solution:
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))
Answer
Solution:
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".
Answer
Solution:
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;
Answer
Solution:
check variable is set use,
$data = array();
if(isset($products))
$data['products'] = $products;
...
return View('product.index', $data);
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: gd library extension not available with this php installation.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.