简体   繁体   中英

Laravel 5: sort products by categories,

I dont have a lot of experience with laravel but i am trying to sort my products on categories... I can already sort all products but not by categorie.

Could you guys help me out?

Model:

class Product extends Model {


protected $table = 'products';


public function getCategoriesListAttribute(){
    return $this->categories()->lists('id');
}


public function categories(){
    return $this->belongsToMany('App\Modules\Webshop\Models\ProductCategory', 'category_product', 'product_id', 'category_id')->withTimestamps();
}}

Products table:

    Schema::create('products', function(Blueprint $table)
    Schema::create('category_product', function(Blueprint $table)
    Schema::create('product_categories', function(Blueprint $table)
    Schema::create('product_tag', function(Blueprint $table)

Controller:

class ProductsController extends Controller {

    public function __construct()
    {
        //$this->middleware('auth');
    }

    public function index()
    {
        $viewmodel = array(
            "categories"=> ProductCategory::where('visible', '1')->get(),
            "products" => Product::has('categories')->get(),
            "page"=>Page::where('href','=', '/')->first(),
        );
        return view('Webshop::frontend.view.products.index', $viewmodel);
    }

}

The index() shows only the products who have any category.

If you guys need more information feel free to ask :)

EDIT:

I am trying this in my controller.

 public function index()
{
    $viewmodel = array(
        "products" => Product::with('categories')->where('category_id','23'),
    );
    return view('Webshop::frontend.view.products.index', $viewmodel);
}

and this:

  $viewmodel = array(
        "products" => Product::leftJoin('category_product', 'category_product.product_id', '=', 'products.id')
                        ->leftJoin('product_categories', 'product_categories.id', '=', 'category_product.category_id')
                        ->where('category_id', 23)
                        ->first(['products.*']),
    );
    return view('Webshop::frontend.view.products.index', $viewmodel);

I have a category with id 23.

Well, if you really mean 'sort', use orderBy() query builder method. If you need to order by some data, located in another table, use join() to include those fields into your query. Pay attention, that when using join() in Eloquent Builder, you need to also use select("products.*") or Laravel will try to fetch incorrect columns into your query result.

If you need to group query result by categories, use group() method of the query result's collection. You can group by category_id field.

Fixed it!

I added this to my model

 public function scopeGetCategorieSlug($query, $category_slug)
{
    return $query->leftJoin('category_product', 'category_product.product_id', '=', 'products.id')
                ->leftJoin('product_categories', 'product_categories.id', '=', 'category_product.category_id')
                ->where('product_categories.slug', $category_slug)
                ->get(['products.*']);
}

and this to my controller

public function show($category_slug)
{
    $viewmodel = array(
        "categories"=> ProductCategory::where('visible', '1')->get(),
        "products" => Product::getCategorieSlug($category_slug),
        "page"=>Page::where('href','=', '/')->first(),
    );
    return view('Webshop::frontend.view.products.index', $viewmodel);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM