Merhaba,
Product Model:
class Product extends Model
{
protected $fillable = ["name", "name_second_line", "sub_name", "slug", "url", "code", "description", "keywords", "wide_description", "additional", "image", "suggested", "user_id", "how_to_order", 'sort', 'status', 'version_id', 'date', 'safe_id','package', 'material_id','surface'];
public function categories(){
return $this->belongsToMany(Category::class, 'product_categories', 'product_id', 'category_id');
}
public function versions()
{
return $this->belongsTo(Version::class, 'version_id');
}
...}
Category Model:
class Category extends Model
{
protected $fillable = ["title","slug", "description", "keywords", 'url', 'content', 'catalog', 'type', "category_id", "sort", "image", "status"];
public function products(){
return $this->belongsToMany(Product::class, 'product_categories', 'category_id', 'product_id')->where('products.status', 1);
}
public function categoryProducts(){
return $this->hasMany('App\ProductCategory', "category_id", "id");
}
...}
ProductCategory Model:
class ProductCategory extends Model
{
protected $fillable = ["product_id", "category_id"];
public function product()
{
return $this->belongsTo('App\Product', "product_id", "id", "version_id")->where('status', 1)->orderBy('sort', 'asc')->orderBy('id', 'asc');
}
public function category()
{
return $this->belongsTo('App\Category', "category_id", "id");
}
...}
CategoryController:
class CategoryController extends Controller
{
public function index($slug, Category $category, Request $request)
{
$category = $category->where('status', 1)->where("slug", $slug)->first();
$categoryProducts = $category->categoryProducts
->sortBy(function($category) {
if(isset($category->product))
return $category->product->sort.'.'.$category->product->id;});
$versions= Version:: all();
$versionss=$request-> query('versi');
return view("front.category.product", [
"category" => $category,
"versions" => $versions,
"categoryProducts"=>$categoryProducts,
"versionss" =>$versionss
]);
}
...}
Product.Blade:
<div class="row">
<div class="col-md-2">
<button type="submit"> Search </button> <br>
@foreach($versions as $version)
<input type="checkbox" value="{{ $version->id }}" id="{{$version->id}}" name="versi" >
<label >{{ $version->name }}</label><br>
@endforeach
</div>
<div class="col-md-10">
<div class="product-list">
<div class="row">
@if(isset($category->categoryProducts) && count($category->categoryProducts)>0)
@if(empty($versin))
@foreach($categoryProducts as $categoryProduct)
@if(isset($categoryProduct->product))
<div class="col-md-6 col-lg-4">
<div class="product">
<a href="{{ StringHelper::product_show_url($categoryProduct->product) }}">
<span class="product-title">{{ $categoryProduct->product->code }}</span>
<span class="name">{{ $categoryProduct->product->name }}</span>
</a>
</div>
</div>
@endif
@endforeach
@else
<p>Seçtiğim markayla eşleşen ürünler</p>
@endif
@else
There is no product.
@endif
</div><!-- /.row -->
</div><!-- /.product-list -->
</div>
</div><!-- /.row -->
</div><!-- /.container -->
</form>
</div><!-- /.main-wrapper -->
...
Site de kategoriye girdigim zaman ürünlerim gelmektedir. Fakat filtrelemeye calistigimda versionlar(audi, bmw,) ilgili ürünleri getiremedim. $versionss=$request-> query('versi'); şununla almaya çalışıyorum seçtiğim değeri fakat ilişkili bir model yapısına sahip olduğumdan bir türlü yoluna koyamadım. İlgili modelim controller ve blade yukarıdaki şekildedir. Yardımlarınızı rica ediyorum.