I want to show product to related category using laravel - 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>

Related

How to fetch user's profile info related to user using laravel?

I want to fetch user's profile info related to user and i have already made relationship b/w user and profile one to one relation but I don't know how to fetch this?
Does anybody have an idea please help me thanks
Database table
Profile table has User_name,phone_number,Job ,user_id
controller
public function viewprofile()
{
return view('viewprofile');
}
profile model
class Profile extends Model
{
protected $table = 'Profiles';
protected $fillable = ['User_name', 'phone_number', 'Job'];
public function user()
{
return $this->belongsTo(User::class);
}
}
User model
class User extends Authenticatable
{
public function profile()
{
return $this->hasOne(Profile::class);
}
}
profile view
<div class="col-12 col-sm-6 col-md-4 d-flex align-items-stretch">
<div class="card bg-light">
<div class="card-header text-muted border-bottom-0">
Digital Strategist
</div>
<div class="card-body pt-0">
<div class="row">
<div class="col-7">
<h2 class="lead"><b>Name: Your Name</b></h2>
<p class="text-muted text-sm"><b>Job: </b> Web Designer / UX / Graphic Artist /
Coffee Lover </p>
<ul class="ml-4 mb-0 fa-ul text-muted">
<li class="small"><span class="fa-li"><i class="fas fa-lg fa-phone"></i></span>
Phone
#: + 800 - 12 12 23 52
</li>
</ul>
</div>
<div class="col-5 text-center">
<img src="../../dist/img/user1-128x128.jpg" alt="" class="img-circle img-fluid">
</div>
</div>
</div>
<div class="card-footer">
<div class="text-right">
<a href="#" class="btn btn-sm bg-teal">
<i class="fas fa-comments"></i>
</a>
<a href="#" class="btn btn-sm btn-primary">
<i class="fas fa-user"></i> View Profile
</a>
</div>
</div>
</div>
</div>
The users profile will be accessible through the relationship and you can get access to it by calling the relationship as a property:
$profile = auth()->user()->profile;
If you need to be able to display it in your view, then just pass it through:
public function viewprofile()
{
return view('viewprofile', ['profile' => auth()->user()->profile]);
}

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) {
}
}

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