nicwek
2 weeks ago
203
14
Laravel

Undefined index: url

Posted 2 weeks ago by nicwek

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

Please sign in or create an account to participate in this conversation.