I'm trying to show products related to the category. if I click this category i want to see related products to this category.? - 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
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");
}
}

Related

list of products in an foreach with an foreach

For school I got a project to make an webshop so I try to make an admin dashboard where if u wanna see the users informatie, u also will see the orders the user placed.
Right now i try this
#foreach($user->order as $order)
<div class='col-sm-12 col-md-6 col-lg-6'>
<div class="card">
<div class="card-header">
<b>Order ID:</b> {{ $order->id }}
</div>
<div class="card-body">
<ul class="list-group list-group-flush">
<li class="list-group-item list-group-item-secondary text-center text-danger"><b>Info:</b></li>
<li class="list-group-item"><b>Datum:</b> {{ $order->date }}</li>
<li class="list-group-item"><b>Prijs:</b> € 200</li>
<li class="list-group-item list-group-item-secondary text-center text-danger"><b>Producten:</b></li>
#foreach($user->order->orderrow as $orderrow)
<li class="list-group-item">{{ $orderrow->product->productname }}</li>
#endforeach
</ul>
</div>
</div>
</div>
#endforeach
but how can i make it that when i want to see the orderrow from an other table then the order table. is able to be worked in an foreach on the users.show.blade.php
Product model
public function orderrows()
{
return $this->hasMany(Orderrow::class)->orderBy('date', 'desc');
}
Orderrow model
public function product()
{
return $this->belongsTo(Product::class);
}
Order model
public function user()
{
return $this->belongsTo(User::class);
}
public function orderrow()
{
return $this->hasMany(Orderrow::class);
}
User model
public function order()
{
return $this->hasMany(Order::class);
}
Try using this code for inner foreach loop.
#foreach($order->orderrow as $orderrow)
<li class="list-group-item">{{ $orderrow->product->productname }}</li>
#endforeach

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 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