How to display subcategory related to category using laravel - 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) {
}
}

Related

i want to hide html code when product is_featured column status all equal to zero using laravel

i am trying to hide html code by if condition when all product featured is zero, html code should not be shown in front page please help me how can i do that ?
controller
public function index()
{
$data = [
'products' => Product::with('productColorGallary')->where('is_featured', 1)->first(),
];
return view('home', $data);
}
html view
#if($products->is_featured == 0)
<div class="col-md-6">
<div class="new-wall-image">
<img src="{{config('wall_master_furishing.file_url').$products->productColorGallary->featured_image}}" alt="">
</div>
</div>
<div class="col-md-6">
<div class="new-wall-descp">
<h2 class="theme-title">New Walls
</h2>
<p>{!!$products->description!!}</p>
<a class="blue-btn-a" href="#">Read More
<i class="fa fa-angle-right">
</i>
</a>
</div>
</div>
#endif
You are getting your products in your database with is_featured = 1. It means that your if condition in your blade will be always false.
Plus, are you trying to get all products or just one ?
If it's many, then :
Your controller
public function index()
{
$products = Product::with('productColorGallary')->get();
return view('home', compact('products');
}
and your blade
#foreach($products as $product)
#if($product->is_featured == 0)
<div class="col-md-6">
<div class="new-wall-image">
<img src="{{config('wall_master_furishing.file_url').$product->productColorGallary->featured_image}}" alt="">
</div>
</div>
<div class="col-md-6">
<div class="new-wall-descp">
<h2 class="theme-title">New Walls
</h2>
<p>{!!$products->description!!}</p>
<a class="blue-btn-a" href="#">Read More
<i class="fa fa-angle-right">
</i>
</a>
</div>
</div>
#else
SOMETHING HERE IF PRODUCT IS FEATURED
#endif
SOMETHING HERE COMMONS TO BOTH FEATURED AND NOT FEATURED
#endforeach
Products is an array key, so you can use like $data['products'] etc..
But if you create a collection/object like the following should be work:
public function index()
{
// first will return the first matched item from db
// and you will get only is_featured = 1
$products = Product::with('productColorGallary')->where('is_featured', 1)->first();
return view('home', compact('products');
}

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");
}
}

I want to display related product based on subcategory using 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'));
}

Resources