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

R0.IT's avatar
Level 1

"Undefined variable: sort"

I want to order products by price and I have some issues, I have this route:

Route::get('shop/{category_url}/','SortController@sort')

controller:

public function sort( $category ,Request $request )}
       if ($category1 = Categorie::where('url', '=', $category)->first()) {
           $sort = $request->get('sort', 'asc');
           $products = Product::where('categorie_id', $category1->getAttribute('id'))-orderBy('price', $sort)->get();
          $sort = $sort == 'asc' ? 'desc' : 'asc'; // to use in sort link
           return view('content.products', compact('products', 'sort'));
       }
    }

view:

        
            @if ($category)
        <h2 class="text-center">{{$category['title']}} </h2>
        <center>

<a href="{{ url('shop/'.$category['url'])}}?sort={{$sort}}">Sort {{$sort === 'asc' ? 'ascending' : 'descending'}}</a>

right now I got an error "Undefined variable: sort" I want to create 2 link one to ASC and the other for DESC

0 likes
12 replies
Snapey's avatar

You get undefined variable where?

R0.IT's avatar
Level 1

When im the url that in the route

Snapey's avatar

When you get an error it says error in file X at line Y This tells you if the issue is in the controller or in the view. Show the error dump

R0.IT's avatar
Level 1

Undefined variable: sort (View: C:\xampp5\htdocs\myshop\resources\views\content\products.blade.php)

Snapey's avatar

And do you use this same blade template from your unsorted controller?

R0.IT's avatar
Level 1

The products.blade is unsorted, when click "sort" it should sort the products

Snapey's avatar

You did not answer the question.

If you load this view without coming through the SortController then obviously $sort is not initialised. You need to pass $sort through from the other controller also.

Or do as I suggested initially and do it all in the one controller. There is no need for separate routes for sorted and unsorted.

R0.IT's avatar
Level 1

But it loaded the sort with the view

 return view('content.products', compact('products', 'sort'));

Can u explain how to to pass $sort through from the other controller also?

R0.IT's avatar
Level 1

Or how to do it one controller? this is my shop controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Categorie;
use App\Product;
use App\Order;
use Cart, Session;

class ShopController extends MainController
{
   public function categories(){
       
   self::$data['categories']=Categorie::all()->toArray();
   self::$data['title']=self::$data['title'].'| Shop Categories';

   return view('content.categories', self::$data);
   }
   
   public function products($category_url){
       
    Product::getProducts($category_url, self:: $data);
    return view ('content.products', self::$data);
   }
   
   public function item($category_url, $product_url){
       
       Product::getItem($product_url, self::$data);
     return view ('content.item', self::$data);
  
}


public function addToCart(Request $request){
    Product::addToCart($request['id']);
    
}
public function checkout(){
    $cartCollection= Cart::getContent();
    $cart=$cartCollection->toArray();
    sort($cart);
    self::$data['cart']=$cartCollection->toArray();
    self::$data['title']=self::$data['title'].'| Shop checkout';
    return view('content.checkout', self::$data);
    
}


public function updateCart(Request $request){
  Product::updateCart($request);
    
}
public function clearCart(){
    
    Cart::clear();
    return redirect('shop/checkout');
}

public function order(){
    
    if( ! Session::has('user_id') ){
        Session::flash('sm','You must sign in to place your order');
        return redirect('user/signin?rnTo=shop/checkout');
    }
    else{
        if (Cart::isEmpty() ){
            return redirect('shop');
            
        }
        else{
            Order::save_order();
            return redirect('shop');
        }
    }
}




}




R0.IT's avatar
Level 1

I tried to use one controller:


   public function products($category_url, Request $request){
        Product::getProducts($category_url, self:: $data);
        if ($category1 = Categorie::where('url', '=', $category_url)->first()) {
           $sort = $request->get('sort', 'asc');
           $products = Product::where('categorie_id', $category1->getAttribute('id'))->orderBy('price', $sort)->get();
          $sort = $sort == 'asc' ? 'desc' : 'asc'; // to use in sort link
           return view('content.products', self::$data , compact('products', 'sort'));
       
        
   }
   }

this is the route:

Route::get('shop/{category_url}','ShopController@products');

this is link fromt the view:

    <a href="{{ url('shop/'.$category['url'])}}?sort=DESC" style="color:black"> High to low</a> |
    <a href="{{ url('shop/'.$category['url'])}}?sort=ASC" style="color:black">Low to high</a>

but when I click on that link the order is not changing.

Snapey's avatar

You will have to debug the controller method and see if you are receiving the sort order and if that is making the desired difference to your query

what is self::$data ?

tpaksu's avatar

Can you try this:

return view('content.products', array_merge(self::$data, compact("sort","products")));

Please or to participate in this conversation.