R0.IT's avatar
Level 1

order by field

Hi, I want to add order by price section to my products list, I'm new in Larvel and I don't know from where to start. So I was thinking to add to ProductController this function:

public function sortBy(){
 
       
       $product_sort = Product::orderBy('price', 'DESC')->get();
    }

but I'm not sure how to continue from there and how to show it in the view.

is someone could give me an advice? many thanks!

0 likes
16 replies
bashy's avatar

I work with CRMs and data systems a lot and the following is something that I do to solve this.

Instead of having a separate method, just use the index one (where you list the data) and pass query strings to it.

public function index(Request $request)
{
    $sort = $request->get('sort', null);

    $products = Product::when($sort, function ($query) use ($sort) {
        // can use the $sort var here if you wish to pass the column or asc/desc
        $query->orderBy('price', 'desc');
    })->get();
}

Then you can just sort it with a link or form depending on what your front-end is like.

<a href="{{ route('products.index', ['sort' => 'price']) }}">Sort by Price</a>
bastman69's avatar

Laravel fortunately give you the flexibility to do the same thing with different ways.

  1. You Can Do it in a method in your model query
$items = Item::sortBy('price', 'desc')->get()
  1. You can do it in the controller.
  2. You can do it in your view in the foreach loop
@foreach($items->sortByDesc('price') as $item)
code here
@endforeach 

And many more. You should always be Advised from the Original Documentation. It contains everything. https://laravel.com/docs/5.4/collections#method-sort

Why don't you follow the series Laravel 5.4 from scratch? to get started.

bashy's avatar

@bastman69 Note that that collections link is for collections. Also, they wanted to know more how to display it than how to sort the data.

mikevrind's avatar

@bashy Didn't know about the when() method, looks nice! Is this method not documented?

R0.IT's avatar
Level 1

@bashy Thanks, this is my controller, I couldn't fint how to combine it:

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

@R0.IT What's your getProducts() method?

@mikevrind Not sure where or if it is but I use it all the time for conditional queries :)

R0.IT's avatar
Level 1

public function products()

bashy's avatar

Huh?

public function products($category_url)
{
    Product::getProducts($category_url, self:: $data);

    return view ('content.products', self::$data);
}
R0.IT's avatar
Level 1

this is the one that shows the products

bashy's avatar

Yeah but what does getProducts do? Just need to know how to tell you to format your conditional order by.

R0.IT's avatar
Level 1
   
    static public function getProducts($category_url, &$data){
        
        $data['products']=$data['category']=[];
        
        
        if ($category=Categorie::where('url','=', $category_url)->first()){
            
            $category= $category->toArray();
            $data['category']=$category;
            $data['title']=$data['title']. ' | ' . $category['title'];
            
            
            if ($products=Categorie::find( $category['id'])->products){
                
                $data['products']= $products->toArray();
            }
        }
bashy's avatar

I had one look at the above code and lost interest :(

2 likes

Please or to participate in this conversation.