Undefined index: url How do I initialize URL variable? I am implementing a brand filter for my e-commerce and i get this error when trying to load a filter.
These are my controller functions:
public function allproducts(Request $request, $url)
{
$products = Product::take(50)->paginate(15);
$categories = Category::whereNull('parent_id')->get(); //where(['url'=>$url,'parent_id'])
if (!empty($_GET['brand'])) {
$brandArray = explode('-', $_GET['brand']);
$products = $products->whereIn('products.product_brand', $brandArray);
}
$brandArray = Product::select('brand')->groupBy('brand')->get();
//$coArray = array_flatten(json_decode(json_encode($colorArray),true));
return view('product.all')->with(compact(['allProducts' => $products, 'categories' => $categories, 'url'
=> $url, 'brandArray' => $brandArray]));
}
public function filter(Request $request)
{
$data = $request->all();
/*echo "<pre>";
print_r($data);*/
$brandUrl = "";
if (!empty($data['brandFilter'])) {
foreach ($data['brandFilter'] as $brand) {
if (empty($brandUrl)) {
$brandUrl = "&brand=" . $brand;
} else {
$brandUrl .= "-" . $brand;
}
}
}
$finalUrl = "products/" . $data['url'] . "?" . $colorUrl;
return redirect::to($finalUrl);
}
I just removed compact function from allproducts method.
public function allproducts(Request $request, $url)
{
$products = Product::take(50)->paginate(15);
$categories = Category::whereNull('parent_id')->get(); //where(['url'=>$url,'parent_id'])
if (!empty($_GET['brand'])) {
$brandArray = explode('-', $_GET['brand']);
$products = $products->whereIn('products.product_brand', $brandArray);
}
$brandArray = Product::select('brand')->groupBy('brand')->get();
//$coArray = array_flatten(json_decode(json_encode($colorArray),true));
return view('product.all')->with([
'allProducts' => $products,
'categories' => $categories,
'url' => $url,
'brandArray' => $brandArray
]);
}
public function filter(Request $request)
{
$data = $request->all();
/*echo "<pre>";
print_r($data);*/
$brandUrl = "";
if (!empty($data['brandFilter'])) {
foreach ($data['brandFilter'] as $brand) {
if (empty($brandUrl)) {
$brandUrl = "&brand=" . $brand;
} else {
$brandUrl .= "-" . $brand;
}
}
}
$finalUrl = "products/" . $data['url'] . "?" . $colorUrl;
return redirect::to($finalUrl);
}
@nicwek You don't send url to public function filter(Request $request)
So here is a problem
$finalUrl = "products/" . $data['url'] . "?" . $colorUrl;
Where do you call that method filter?
The focus of the error occurs in the filter method when assigning values to $finalUrl variable
@nicwek I know, post your view where you set those data for request.
@wingly It will be propably next error.
@nicwek then update your code to reflect the change so we can help you with the rest of the remaining bugs
The view...
<div class="cat-brand">
<div class="sec-title">
<h6>Brands</h6>
</div>
<form action="{{ url('/products/filter')}}" method="POST">
@if(!empty($url))
<input name="url" value="{{ $url }}" type="hidden">
@endif
@csrf
<div class="brand-box">
@foreach($brandArray as $brand)
@if(!empty($_GET['brand']))
<?php $brandrArr = explode('-',$_GET['brand']) ?>
@if(in_array($brand,$brandArr))
<?php $brandcheck="checked"; ?>
@else
<?php $brandcheck=""; ?>
@endif
@else
<?php $brandcheck=""; ?>
@endif
<ul class="list-unstyled">
<li><input name="brandFilter[]" onchange="javascript:this.form.submit();" id="{{ $brand
}}" value="{{ $brand }}" type="checkbox" {{ $brandcheck }}> <span>{{
$brand }}</span>
</li>
</ul>
@endforeach
</div>
</form>
</div>
@michaloravec , Kindly explain to me what you mean by this, You don't send url to public function filter(Request $request) in:
$finalUrl = "products/" . $data['url'] . "?" . $colorUrl;
@nicwek In your view you have
@if (! empty($url))
<input name="url" value="{{ $url }}" type="hidden">
@endif
But what happen if that this condition ! empty($url) is false?
In that case you don't send url to your request.
So this throw an error
$finalUrl = "products/" . $data['url'] . "?" . $colorUrl;
Because in array $data is undefined index url.
if (array_key_exists('url', $data)) {
$finalUrl = "products/" . $data['url'] . "?" . $colorUrl;
} else {
$finalUrl = 'something default'; // change your default url if there isn't rul in your `$data` array.
}
@michaloravec , thanks for the response. I tried it and it gets rid of the error and in its place, I get a blank page, that is when I insert a default route
@nicwek That's what you have to solve for yourself, i don't know what you want to expect.
Please sign in or create an account to participate in this conversation.