Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nicwek's avatar

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);
}
0 likes
14 replies
MichalOravec's avatar

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);
}
MichalOravec's avatar

@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?

nicwek's avatar

The focus of the error occurs in the filter method when assigning values to $finalUrl variable

wingly's avatar

@nicwek then update your code to reflect the change so we can help you with the rest of the remaining bugs

nicwek's avatar

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 }}>&nbsp;&nbsp;<span>{{ 
                                  $brand }}</span>          
                                 </li>
                                   
                                </ul>

                            @endforeach

                        </div>
                        </form>
                    </div>
nicwek's avatar

@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;
MichalOravec's avatar

@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.
}
nicwek's avatar

@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

Please or to participate in this conversation.