Laravel: Filter threads by clickable categories - laravel

I made a page with a categories list on the left and threads on the right side. List of categories are dynamically displayed with CRUD in different controller as threads. And all that works.
ForumThread controller index method:
public function index()
{
$threads = ForumThread::orderBy('id', 'desc')->paginate(10);
$categories = ForumCategory::with('threads')->get();
return view('forum.thread.index', compact('threads', 'categories'));
}
threads belongs to categories of course and view with categories part look like this:
<div class="col-md-3">
<ul class="list-group">
<a href="{{route('forum.index')}}" class="list-group-item">
<span class="badge">{{$threads->count()}}</span>
All categories
</a>
#foreach ($categories as $category)
<a href="#" class="list-group-item">
<span class="badge">{{$category->threads->count()}}</span>
{{$category->name}}
</a>
#endforeach
</ul>
</div>
The thing I want is to set a href link to go to threads that belongs to that specific category that is clicked.
I presume that I need a new method for this (I presume because I don't know how to make this happened in index method) so I made one:
public function showThreads($id)
{
$showThreads = ForumCategory::with('threads')->findOrFail($id);
return view('forum.thread.index', compact('showThreads'));
}
route:
Route::get('/forum/category/{id}', 'ForumThreadController#showThreads')->name('category.threads');
and when I set id for example '/forum/category/3' and try function dd(showThreads->thread); it shows me an array of threads which belongs to that category id.
But now I don't know how to implement this in this foreach loop that already working for category list. I tried like this:
<div class="col-md-3">
<ul class="list-group">
<a href="{{route('forum.index')}}" class="list-group-item">
<span class="badge">{{$threads->count()}}</span>
All categories
</a>
#foreach ($categories as $category)
<a href="{{ route('category.threads', $showThreads->id) }}" class="list-group-item">
<span class="badge">{{$category->threads->count()}}</span>
{{$category->name}}
</a>
#endforeach
</ul>
</div>
but it shows me error: Undefined variable: threads
Any help?

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.

I am trying to click on a category and products will show accordingly, but its not working

Controller:
public function shopfront(){
$products=Product::all();
$categories=Category::all();
return view('shop.index',compact('products','categories'));
}
index.blade.php:
<div class="sidebar-widget">
<h4 class="pro-sidebar-title">Categories</h4>
<div class="sidebar-widget-list mt-30">
<ul>
#foreach ($categories as $category)
#foreach ($products as $product)
#if ($category->id==$product->category->id)
<li>
<div class="sidebar-widget-list-left">
<input type="checkbox"> <a href="">{{ $category->name }}
{{-- <span>4</span> --}}
</a>
<span class="checkmark"></span>
</div>
</li>
#endif
#endforeach
#endforeach
</ul>
</div>
</div>
I do not know how to achieve this, will highly appreciate some help or i sample that i can work with
Image
i don't know how you want to achieve it but let me give you an idea if you want it in this way:
when click on a category then it should show all products of that category:
in your blade file
#foreach($categories as $category)
<li class="nav-item">
<a class="nav-link" href="{{route('category.products',$category->name)}}"> {{$category->name}}</a>
</li>
#endforeach
you should create a route named category.products as below in web.php
Route::get('category/{name}','YourController#yourmethod')->name('category.products');
controller
public function CategoryProducts($name){
$category = Category::where('name',$name)->first();
$products = $category->products;
return view('front-end.categoryWise-products',compact('products'));
}
you can customize it according to your requirements.

Is Nested GroupBy supported in Laravel?

I have a categories table with the fields "id", "category_id", "name" where "category_id" is the id of the parent category. My requirement is that I need to group the categories like below.
Europe
- England
Asia
- India
- Kerala
- Kochi
Note - Here Europe,England,Asia,India,Kerala,Kochi are categories. Asia is the parent category of India and India is the parent category of Kerala and Kerala is the parent category of Kochi.
Is there some nested groupBy solution?
Assuming that it's a one-to-many self-referencing relationship i.e. one category can only belong to some other category (single parent)
And your Category model should look something like this:
public function parent() {
return $this->belongsTo('App\Category');
}
public function children() {
return $this->hasMany('App\Category');
}
In your main blade template, you have:
<ul class="navbar-nav">
#foreach ($menu_items as $item)
#include('menu', $item)
#endforeach
</ul>
Where $menu_items is the list of all parent categories ( categories where category_id is null or 0 )
And your menu blade template:
#if ( $item->children->isNotEmpty() )
<li class="nav-item {{ $item->parents->isNotEmpty() ? 'dropdown-submenu' : 'dropdown' }}">
<a class="nav-link dropdown-toggle" href="{{ $item->slug ? url($item->slug) : 'javascript:void(0);' }}" id="navbarDropdown{{ $item->id }}" role="button" data-toggle="hover">
{{ $item->title }}
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown{{ $item->id }}">
#foreach ($item->children as $item)
#include('menu', $item)
#endforeach
</ul>
#else
<li class="nav-item">
<a class="nav-link" href="{{ $item->slug ? url($item->slug) : 'javascript:void(0);' }}">{{ $item->title }}</a>
#endif
</li>
I literally pasted this code from a project I worked on recently to give you an idea about nested drop-down, you've to work on the styling part yourself. But this code is enough to give you a proper understanding.
PS: this all untested. let me know if something doesn't work

undefined variable when i called it on a header view in laravel

My route is:
Route::get('header','HomeController#header')->name('header');
My functionality is:
public function header()
{
$header=ParentCategory::latest()->get();
return view('includes.header',compact('header'));
}
My view foreach loop is here:
#foreach($header as $pc)
<li class=" aligned-left parent dropdown " >
<a href="index7fa3.html?route=product/category&path=18"
class="dropdown-toggle" data-toggle="dropdown">
<span class="menu-title">{{$pc->name}}</span><b class="caret"></b></a>
#endforeach
I don't want to write their route which name is header, mention in browser I want to work my foreach loop without route name, header.
You can check for your variable first before looping through it:
#if($header)
#foreach($header as $pc)
<li class=" aligned-left parent dropdown " >
<a href="index7fa3.html?route=product/category&path=18"
class="dropdown-toggle" data-toggle="dropdown">
<span class="menu-title">{{$pc->name}}</span><b class="caret"></b>
</a>
</li>
#endforeach
#endif

Foreach loop of Eloquent relationship in Laravel view then changes any further results below it

I have a Eloquent relationship defined in the a Model.
In my Controller I am getting some profiles like so:
$profiles = $this->profileRepo->paginate('10');
In my view I make use of a relationship in the Profile model which gets the related images and user this in the foreach loop.
But then after this I try to get the Profile->nick_name and it won't work. I did a dd() and it shows the Model has now changed from Profile to the Image..
#foreach($profiles as $row)
<div>
#foreach($row->image->where('profile_picture', '1') as $row)
<a href="$row->file">
<i class="fa fa-plus"></i>
</a>
#endforeach
</div>
<div>
//If this was ^^^Above^^^ the foreach() loop then it works fine :)
<h3>{{ $row->nick_name }}</h3>
<p>{{ $row->about }}</p>
</div>
#endforeach
Not sure why that happens or how to prevent it.
Your inner foreach loop overrides the $row variable.
Use a different variable in your inner loop:
#foreach($profiles as $profile)
<div>
#foreach($profile->image->where('profile_picture', '1') as $image)
<a href="{{ $image->file }}">
<i class="fa fa-plus"></i>
</a>
#endforeach
</div>
<div>
<h3>{{ $profile->nick_name }}</h3>
<p>{{ $profile->about }}</p>
</div>
#endforeach

Resources