Hi, I'm facing an issue to show only the products that realted to category , (it shows all the products that exists
the conroller:
class SortController extends MainController{
public function sortByASC( $category_url,Request $request)
{
$sort = $request->get('sort', 'asc');
if ($category=Categorie::where('url','=', $category_url)){
$products = Product::orderBy('price', $sort)
->get();
return view('content.sort')->with('products', $products) ;
}}
view
<form id="order-product-form" method="get" action="{{url('shop/{category_url}/sort=ASC')}}"enctype="multipart/form-data">
@if ($products)
@foreach($products as $product)
<div class="col-md-12">
<h2>{{ $product['title']}}</h2>
model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Cart;
use Session;
class Product extends Model {
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();
}
}
}
static public function getItem($product_url, &$data){
$data['product']=[];
if ($product =self::where('url','=',$product_url)->first()){
$data['product']= $product->toArray();
}
}
static public function addToCart($id){
if($product= self::find($id) ){
$product=$product->toArray();
if(!Cart::get($id)){
Session::flash('sm',$product['title'].'added to cart!');
Cart::add($id, $product['title'], $product['price'],1,[]);
}
}
}
static public function updateCart($request){
if ( !empty($request['id'])&& !empty($request['op']) ){
if( $product= Cart::get($request['id'])){
$product=$product->toArray();
if($request['op']=='plus'){
Cart::update($request['id'],
['quantity'=>1]);
}
elseif($request['op']=='minus'){
if($product['quantity']-1==0){
Cart::remove($request['id']);
}
else{
Cart::update($request['id'],
['quantity'=>-1]);
}
}
}
}
}
static public function save_product($request){
$image_name='noimage.jpg';
if ($request->hasFile('image') && $request->file('image') ->isValid() ) {
$file= $request->file('image');
$image_name= date('Y.m.d.H.i.s').'-'.$file->getClientOriginalName();
$request->file('image')->move(public_path(). '/images' , $image_name);
}
$product= new self();
$product->categorie_id=$request['categorie_id'];
$product->title=$request['title'];
$product->article= $request['article'];
$product->url= $request['url'];
$product->image=$image_name;
$product->price=$request['price'];
$product->save();
Session::flash('sm','Product has been save');
}
static public function updateProduct($request, $id){
$image_name='';
if ($request->hasFile('image') && $request->file('image') ->isValid() ) {
$file= $request->file('image');
$image_name= date('Y.m.d.H.i.s').'-'.$file->getClientOriginalName();
$request->file('image')->move(public_path(). '/images' , $image_name);
}
$product=self::find($id);
$product->categorie_id=$request['categorie_id'];
$product->title=$request['title'];
$product->article= $request['article'];
$product->url= $request['url'];
if($image_name){
$product->image=$image_name;
}
$product->image=$image_name;
$product->price=$request['price'];
$product->save();
Session::flash('sm','Product has been updated');
}
static public function sortBy(&$data){
$data['price']=DB::table('products')
->join('categories', 'products.categorie_id' ,'=','categories.id')
->select ('products.*', 'categories.url as curl')
->orderBy('products.price','desc')->get()->toArray();
$data['price']=array_map(function($item){
return (array) $item;
},$data['price']);
}
}