I want to display related product based on subcategory using laravel - laravel

I am trying to show related products based on a subcategory. in this menu when I click the parent category it shows products but on the click of subcategory product is not showing, I want to show product when I click subcategory.
Does anybody have any idea?
thanks in advance for your help :)
category table
]
product table
product has id product_name ,product_image,price,cate_id
video
please watch video for better understanding thanks
https://www.loom.com/share/5b91deedac154c98bc275c9edcd3b773
Category Model
public function category()
{
return $this->hasMany('App\category','p_id');
}
controller
public function procateg(Request $request){
$cate_name=$request->cate_name;
$product=DB::table('category')
->leftjoin('products','category.id','=','products.cate_id')
->where('category.cate_name','=',$cate_name)->paginate('8');
return view('front_end/products',compact('product'));
}
HTML view
<div class="nav-panel__nav-links nav-links">
<ul class="nav-links__list">
#foreach(App\category::with('category')->where('p_id',0)->get() as $firstmenu)
<li class="nav-links__item nav-links__item--has-submenu ">
<a id="cate" class="nav-links__item-link" href="{{url('/product/'.$firstmenu-
>cate_name)}}">
<div class="nav-links__item-body">
{{$firstmenu->cate_name}}
<svg class="nav-links__item-arrow" width="9px" height="6px">
<use xlink:href="{{url('public/assets/images/sprite.svg#arrow-rounded-down-9x6')}}">
</use>
</svg>
</div>
</a>
<div class="nav-links__submenu nav-links__submenu--type--megamenu nav-links__submenu--
size--nl">
<!-- .megamenu -->
<div class="megamenu ">
<div class="megamenu__body">
<div class="row">
#foreach($firstmenu->category as $secondmenu)
<div class="col-6">
<ul class="megamenu__links megamenu__links--level--0">
<li class="megamenu__item megamenu__item--with-submenu ">
<a href="{{url('product'.'/'.$firstmenu->cate_name.'/'.$secondmenu->cate_name)}}">
{{$secondmenu->cate_name}}</a>
<ul class="megamenu__links megamenu__links--level--1">
#foreach($secondmenu->category as $thirdmenu)
<li class="megamenu__item"><a href="{{url('product'.'/'.$firstmenu-
>cate_name.'/'.$secondmenu->cate_name.'/'.$thirdmenu->cate_name)}}">
{{$thirdmenu->cate_name}}</a>
</li>
#endforeach
</ul>
</li>
</ul>
</div>
#endforeach
</div>
</div>
</div>
<!-- .megamenu / end -->
</div>
</li>
#endforeach
</ul>
</div>
Route
Route::get('product','FrontController#product');
Route::get('product/{cate_name}','FrontController#procateg');

Category Model
public function category()
{
return $this->hasMany('App\category','p_id','id');
}
controller
public function procateg(Request $request){
$cate_name=$request->cate_name;
$product = DB::table('category')
->Join('products','category.id','=','products.cate_id')
->where('category.cate_name','=',$cate_name)->get();
return view('front_end/products',compact('product'));
}

Related

Laravel8 Property [Eng2] does not exist on this collection instance

I am facing a strange problem: I have 2 pages with similar structure relating to same DB, the first one displaying a grid of products working perfectly, the second the item detail giving me this error. I can get the all JSON string {{$wine}} on the page but no way to extract elements with {{$wine->Eng2}} or {{$wine['Eng2']}}
#extends('layouts.layout')
#include('partials.sidebar')
<div class="detail-container">
<img src="/img/9122705276958.png" alt="" />
<ul>
<!--<li>{{$wine}}</li>-->
<li>{{$wine->Eng2}}</li>
<li></li>
</ul>
<!--<div class="addCart">
<i class="fas fa-shopping-cart"></i>
</div>
<ul class="side-icons">
<span><i class="fas fa-search"></i></span>
<span><i class="far fa-heart"></i></span>
<span><i class="fas fa-sliders-h"></i></span>
</ul>
</div>
<div class="bottom">
<ul>
<li></li>
<li></li>
</ul>
<ul class="price">
<li>Retail price: Ntd </li>
<li>Member price: Ntd </li>
<li>VIP price down to: Ntd </li>
</ul>
</div> -->
</div>
public function detail($id) {
$wine =Wine::where('ART','=',$id)->get();
return view('detail', [
'wine' => $wine
]);
}
Route::get('wines', [WineController::class,'index']);
Route::get('/wines/{id}',[WineController::class,'query']);
Route::get('detail/{id}', [WineController::class,'detail']);
Try to use
$wine = Wine::where('ART','=',$id)->first();
instead of
$wine = Wine::where('ART','=',$id)->get();
first() will get you the first element in the collection but get() will get you the whole collection, so you can use first() if the collection has one element, or you have to use foreach in the view.

How to display subcategory related to category using laravel

I am trying to display subcategory to related category in this menu firsrt category has been displayed but i have to display subcatory to relate category ?
Database
category table
front page
controller
public function index(){
$category=DB::table('category')->where('p_id',0)->get();
return view('front_end/index',compact('category'));
}
html view
<div class="nav-panel__nav-links nav-links">
<ul class="nav-links__list">
#foreach($category as $firstmenu)
<li class="nav-links__item nav-links__item--has-submenu ">
<a id="cate" class="nav-links__item-link" href="
{{$firstmenu->cate_name}}">
<div class="nav-links__item-body">
{{$firstmenu->cate_name}}
<svg class="nav-links__item-arrow" width="9px" height="6px">
<use xlink:href="public/assets/images/sprite.svg#arrow-rounded-down-9x6"></use>
</svg>
</div>
</a>
<div class="nav-links__submenu nav-links__submenu--type--megamenu nav-links__submenu--
size--nl">
<!-- .megamenu -->
<div class="megamenu ">
<div class="megamenu__body">
<div class="row">
<div class="col-6">
<ul class="megamenu__links megamenu__links--level--0">
<li class="megamenu__item megamenu__item--with-submenu ">
/sub category /
T-shirts
<ul class="megamenu__links megamenu__links--level--1">
/sub category /
<li class="megamenu__item">short paint
</li>
</ul>
</li>
</ul>
</div>
</div>
</div>
</div>
<!-- .megamenu / end -->
</div>
</li>
#endforeach
</ul>
</div>
so at first you need to fetch this subcategory somehow. You can use eloquent relationship if defined or simple join like so:
DB query:
$category = DB::table('category')
->join('subcategory', 'subcategory.category_id', '=','category.id')
->where('p_id',0)
->get();
Eloquent(if category have more than one subcategory):
Category Model:
public function subcategory()
{
return $this->hasMany(SubCategory::class, 'category_id', 'id');
}
Controller
$category = Category::with('subcategory')->get();
And then just get the name or some other value of subcategory in view (important - if there could be more subcategories related to your category than just one you have to use loop to itereate through all subcategories):
{{$category->subcategory->name}}
Or
#foreach($category->subcategory as $sub_cat)
{{$sub_cat->name}}
#endforeach
For more informations you should check Laravel documentation:
https://laravel.com/docs/6.x/eloquent-relationships
As you have posted the table structure
Your relationship will be like
public function subCategories(){
return $this->hasMany(SELF::class, 'p_id', 'id');
}
public function parentCategories(){
return $this->hasMany(SELF::class, 'p_id', 'id');
}
public scopeParent($query){
return $query->whereNull('p_id');
}
public scopeChild($query){
return $query->whereNotNull('p_id');
}
And you should be able to get the categories like
$categories = Category::parent()->get();
and then something like this
foreach($categories->with('subCategories')->get() as $category) {
foreach($category->subCategories as $subCategory) {
}
}

I want to show product to related category using laravel

I'm trying to show products related to the category. In my menu I have a list of categories if I click this category i want to see related products to this category. I'm just learning laravel can somebody help me out..
DATABASE
-category: ID, cate_name ,p_id
-products: has Category_id
category model
namespace App;
use Illuminate\Database\Eloquent\Model;
class category extends Model
{
public $table = "category";
public function childs()
{
return $this->hasMany('App\category','p_id');
}
}
category view
<div class="nav-panel__nav-links nav-links">
<ul class="nav-links__list">
#foreach(App\category::with('childs')-
>where('p_id',0)->get() as $firstmenu)
<li class="nav-links__item nav-links__item--has-submenu ">
<a id="cate" class="nav-links__item-link" href="{{$firstmenu-
>cate_name}}">
<div class="nav-links__item-body">{{$firstmenu->cate_name}}
<svg class="nav-links__item-arrow" width="9px" height="6px">
<use xlink:href="public/assets/images/sprite.svg#arrow-rounded-down-
9x6"></use>
</svg>
</div>
</a>
<div class="nav-links__submenu nav-links__submenu--type--megamenu
nav-links__submenu--size--nl">
<!-- .megamenu -->
<div class="megamenu ">
<div class="megamenu__body">
<div class="row">#foreach($firstmenu->childs as
$secondmenu)
<div class="col-6">
<ul class="megamenu__links megamenu__links--level--0">
<li class="megamenu__item megamenu__item--with-submenu "> {{$secondmenu->cate_name}}
<ul class="megamenu__links megamenu__links--level-
-1">#foreach($secondmenu->childs as $thirdmenu)
<li class="megamenu__item">{{$thirdmenu->cate_name}}
</li>#endforeach</ul>
</li>
</ul>
</div>#endforeach</div>
</div>
</div>
<!-- .megamenu / end -->
</div>
</li>#endforeach</ul>
</div>
How do I go from here?
Change your code in category class like this :
class category extends Model
{
public $table = "category";
public function products()
{
return $this->hasMany('App\products','p_id');
}
}
Then you can get all you product related to that category like this :
#foreach(App\category::all() as $firstmenu)
#foreach($firstmenu->products())
// All of product in this one category
#endforeach
#endforeach
You need to update your category model
category.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class category extends Model
{
public $table = "category";
public function products()
{
return $this->hasMany('App\product', 'ID', 'p_id');
}
}
category view
<div class="nav-panel__nav-links nav-links">
<ul class="nav-links__list">
#foreach(App\category::with('products')->where('p_id',0)->first()->products as $firstmenu)
<li class="nav-links__item nav-links__item--has-submenu ">
<a id="cate" class="nav-links__item-link" href="{{$firstmenu->cate_name}}">
<input type="hidden" name="cate" value="{{$firstmenu->id}}">
<div class="nav-links__item-body">
{{$firstmenu->cate_name}}
<svg class="nav-links__item-arrow" width="9px" height="6px">
<use xlink:href="public/assets/images/sprite.svg#arrow-rounded-down-9x6"></use>
</svg>
</div>
</a>
<li>
</ul>
</div>

How to dispaly product to related category using laravel [duplicate]

This question already has answers here:
I want to show product to related category using laravel
(2 answers)
Closed 3 years ago.
I'm trying to show products related to the category. In my menu I have a list of categories if I click this category i want to see related products to this category. I'm just learning laravel can somebody help me out..
DATABASE:
-category: id, cate_name ,p_id
-product: has Category_id
Product model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class product extends Model
{
protected $fillable = [ 'id', 'product_name', 'product_price', 'product_image',
'product_brand'];
}
Category model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class category extends Model
{
public $table = "category";
public function childs()
{
return $this->hasMany('App\category','p_id');
}
}
Category view:
<div class="nav-panel__nav-links nav-links">
<ul class="nav-links__list">
#foreach(App\category::with('childs')->where('p_id',0)->get() as $firstmenu)
<li class="nav-links__item nav-links__item--has-submenu ">
<a id="cate" class="nav-links__item-link" href="{{$firstmenu->cate_name}}">
<div class="nav-links__item-body">
{{$firstmenu->cate_name}}
<svg class="nav-links__item-arrow" width="9px" height="6px">
<use xlink:href="public/assets/images/sprite.svg#arrow-rounded-down-9x6"></use>
</svg>
</div>
</a>
<div class="nav-links__submenu nav-links__submenu--type--megamenu nav-
links__submenu--size--nl">
<!-- .megamenu -->
<div class="megamenu ">
<div class="megamenu__body">
<div class="row">
#foreach($firstmenu->childs as $secondmenu)
<div class="col-6">
<ul class="megamenu__links megamenu__links--level--0">
<li class="megamenu__item megamenu__item--with-submenu ">
{{$secondmenu->cate_name}}
<ul class="megamenu__links megamenu__links--level--1">
#foreach($secondmenu->childs as $thirdmenu)
<li class="megamenu__item">{{$thirdmenu->cate_name}}</li>
#endforeach
</ul>
</li>
</ul>
</div>
#endforeach
</div>
</div>
</div>
<!-- .megamenu / end -->
</div>
</li>
#endforeach
</ul>
</div>
Dependiong on your relation type (One-Many, Many-Many, One-One) you can create relations,
you can use this article

I'm trying to show products related to the category. if I click this category i want to see related products to this category.?

I'm trying to show products related to the category. In my menu I have a list of categories if I click this category i want to see related products to this category. I'm just learning laravel can somebody help me out..
database
category id ,cate_name p_id
product has - cate_id
product model
class product extends Model
{
protected $fillable = [ 'id', 'product_name', 'product_price',
'product_image', 'product_brand'];
}
category model
class category extends Model
{
public $table = "category";
public function childs()
{
return $this->hasMany('App\category','p_id');
}
}
html view
<div class="nav-panel__nav-links nav-links">
<ul class="nav-links__list">
#foreach(App\category::with('childs')->where('p_id',0)->get()as $firstmenu)
<li class="nav-links__item nav-links__item--has-submenu">
<a id="cate" class="nav-links__item-link" href="{{$firstmenu->cate_name}}">
<div class="nav-links__item-body">
{{$firstmenu->cate_name}}
<svg class="nav-links__item-arrow" width="9px" height="6px">
<use xlink:href="public/assets/images/sprite.svg#arrow-rounded-down-9x6">
</use>
</svg>
</div>
</a>
<div class="nav-links__submenu nav-links__submenu--type--megamenu nav-
links__submenu--size--nl">
<!-- .megamenu -->
<div class="megamenu ">
<div class="megamenu__body">
<div class="row">
#foreach($firstmenu->childs as $secondmenu)
<div class="col-6">
<ul class="megamenu__links megamenu__links--level--0">
<li class="megamenu__item megamenu__item--with-submenu ">
{{$secondmenu->cate_name}}
<ul class="megamenu__links megamenu__links--level--1">
#foreach($secondmenu->childs as $thirdmenu)
<li class="megamenu__item">{{$thirdmenu->cate_name}}
</li>
#endforeach
</ul>
</li>
</ul>
</div>
#endforeach
</div>
</div>
</div>
<!-- .megamenu / end -->
</div>
</li>
#endforeach
</ul>
</div>
Your product model should look like this:
class Product extends Model
{
public $guarded = [];
public function category(){
return $this->belongsTo("App\Category", "category_id");
}
}
And this should be your Category model "Category.php":
class Category extends Model
{
public $guarded = [];
public function products(){
return $this->hasMany("App\Product", "category_id");
}
}

Resources