I have tables - products and categories with a pivot table. What i need to do is to display the categories on view in a tab and get the respective products.
// Product.php
public function categories(){
return $this->belongsToMany('App\Models\Category');
}
// Category.php
public function products(){
return $this->belongsToMany('App\Product');
}
// IndexController.php
$products = Product::where('status', StatusConstant::PT_ACTIVE)
->with(['joindraw' => function ($query){
$query->where('user_id', $this->user->id);
}])->get();
return view('store.index', ['products' => $products, 'category' => $category]);
and I'm expecting some output like this:
<div class="row">
<div class="col s12">
<ul class="tabs">
<li class="tab"><a class="" href="#tabs1">Category 1</a></li>
<li class="tab"><a class="active" href="#tabs2">Category 2</a></li>
<li class="tab"><a class="" href="#tabs3">Category 3</a></li>
</ul>
</div>
<div id="tabs1" class="col s12">
<div class="contents-tabs">
<div class="table-contents default-table">
//Products in category id 1...
May i know how can i do the filter on blade?
Instead of sending products to the view, consider sending categories with related products to the view.
Controller
$categories = Category::with(['products' => function ($query) {
$query->where('status', StatusConstant::PT_ACTIVE);
$query->with(['joindraw' => function ($query) {
$query->where('user_id', $this->user->id);
}]);
}])->get();
return view('store.index', ['categries' => $categries]);
Now you can loop through each category and access its related products.
Blade
<div class="row">
<div class="col s12">
<ul class="tabs">
#foreach($categries as $category)
<li class="tab"><a class="" href="#{{ $category->id }}">{{ $category->name }}</a></li>
#endforeach()
</ul>
</div>
#foreach($categories as $category)
<div id="{{ $category->id }}" class="col s12">
<div class="contents-tabs">
<div class="table-contents default-table">
<!-- your code goes here -->
<!-- Imagine you wanna list product names. -->
<ul>
#foreach($category->products as $product)
<li>{{ $product->name }}</li>
#endforeach()
</ul>
</div>
</div>
</div>
#endforeach()
</div>
You can group products per categories:
$categoryProducts = [];
foreach($products as $product){
foreach($product->categories as $category){
if(!isset($categoryProducts[$category->id])){
$categoryProducts[$category->id] = [];
}
$categoryProducts[$category->id][$product->id]=$product;
}
}
Then you can show each category products in their tabs with category id.
Related
I have a Category and Weblog for my website.
This is Category Model :
public function weblogs()
{
return $this->belongsToMany(Weblog::class);
}
And this is Weblog Model :
public function categories()
{
return $this->belongsToMany(Category::class);
}
As you can see there is a realation between Weblog and Category.
This is Controller :
$weblogs = Weblog::paginate(9);
return view('index', compact('weblogs'));
And this is my blade for showing weblog items :
#foreach($weblogs as $weblog)
<div class="col-lg-4 col-sm-6">
<div class="blog-item">
<div class="thumbnail">
<a href="#"><img alt=""
src="/Weblog/image/{{ $weblog->image }}"></a>
</div>
<h4>{{ $weblog->name }}</h4>
<ul>
<li>{{ jdate($weblog->created_at)->format('%d %B %Y') }}</li>
<li>سبک زندگی</li>
</ul>
<div class="blog-btn">
بیشتر بخوانید
</div>
</div>
</div>
#endforeach
I want to show category belong to any item of weblog, how can i do this ?
Just access property in your template
<div class="blog-item">
...
#foreach ($weblog->categories as $category)
{{ $category }}
#endforeach
...
</div>
You can also load relations earlier
$weblogs = Weblogs::with('categories')->paginate(9)
You can read more about it here https://laravel.com/docs/7.x/eloquent-relationships#relationship-methods-vs-dynamic-properties and here https://laravel.com/docs/7.x/eloquent-relationships#eager-loading
I have an order list in my admin panel, I want to use tabs to separate the orders based on the status
I defined the statuses in my Model:
public static $statuses = [
'done' => 'Successful Orders',
'canceled' => 'Canceled Orders',
'pending'=> 'Pending Orders'
];
Here are my tabs (bootstrap):
<div class="container-fluid">
<ul class="nav nav-tabs">
<li class="active"><a data-toggle="tab" href="#pending">Pending Orders</a></li>
<li><a data-toggle="tab" href="#done">Successful Orders</a></li>
<li><a data-toggle="tab" href="#canceled">Canceled Orders</a></li>
</ul>
<div class="tab-content">
<div id="pending" class="tab-pane fade in active">
//ORDERS WITH PENDING STATUS
</div>
</div>
<div class="tab-content">
<div id="done" class="tab-pane fade in active">
//ORDERS WITH DONE STATUS
</div>
</div>
<div class="tab-content">
<div id="canceled" class="tab-pane fade in active">
//ORDERS WITH CANCELED STATUS
</div>
</div>
Now I want to display this data on each tab based on status:
#foreach($orders as $order)
{{ $order->id }}
{{ $order::$statuses[$order->status] }}
#endforeach
CONTROLLER
function index() {
if(request()->has('status')) {
$orders = Order::where('status', request('status'))->paginate(5)->append('status', request('status'));
}
return view('orders.index', compact('orders'));
}
View
Pending
Pending
Pending
foreach($orders as $order) {
$order->id;
}
I'm building a Laravel application and I have now added two tables, an "employees" and "employees_category" table. Both of them have an "order" column so that I can set the order of how the categories and employees will be displayed on the website.
When I add an employee, I can assign them to a category. So far so good, I'm able to display the employees in order they have been set, but not the categories.
This is what I have so far:
My EmployeesController:
public function index()
{
$employees = Employee::all()->groupBy('category');
return view('app.employee.index', compact('employees'));
}
And in my blade view:
#foreach ($employees as $category => $workers)
<div class="col text-center mb-6">
<h2>{{ $category }}</h2>
</div>
<div class="row px-4 px-xl-10">
#foreach($workers->sortBy('employee_order') as $worker)
// some content
#endforeach
</div>
#endforeach
This sorts the employees correctly by order-number but not the categories, how can I achieve that?
Why dont you query exactly what you want in your view
public function index()
{
$categories = Category::with('employees')->get();
return view('app.employee.index', compact('categories'));
}
and in the view : (notice what you want is as $category => $workers)
#foreach ($categories as $category)
<div class="col text-center mb-6">
<h2>{{ $category }}</h2>
</div>
<div class="row px-4 px-xl-10">
#foreach($category->employees->sortBy('employee_order') as $worker)
// some content
#endforeach
</div>
#endforeach
The good thing is you can now paginate on the catogories
Im getting all the categories like:
public function index(){
return view('home')
->with('categories', Category::orderBy('created_at', 'desc')->get())
->with('conferences', Conference::orderBy('created_at','desc')->where('status','P')->take(8)->get())
}
Then in the blade file I show the categories like below. But do you know how to order the categories by the number of conferences associated with each category? That is, show first the categories that have more conferences associated with it. Do you know how that can be achieved?
<div class=" d-md-block">
<div class="container">
<div class="row">
<div class="col p-0 m-0">
<ul class="Categories__Menu">
#foreach($categories->take(6) as $category)
<li class="">
{{$category->name}}
</li>
#endforeach
<li><a data-toggle="modal" id="showCategories" data-target=".bd-example-modal-lg" href="">More <i class="fa fa-caret-down" aria-hidden="true"></i></a></li>
</ul>
</div>
</div>
</div>
</div>
Use this.
Category::withCount('conferences')->orderBy('conferences_count', 'desc')->get();
This is the code from CategoriesController:
public function index()
{
$categories = Category::all();
return view('/home', compact('categories'));
}
And this is the code that I'm writing to the home.blade.php
#foreach($categories as $cat)
#endforeach
And then in home.blade.php
I'm getting this error: Undefined variable: categories
Why this happening ? Everything works fine with Articles but not categories and the code is the same.
Home.blade.php
#extends('app')
#section('content')
<div class="col-md-4 pull-right">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Categories</h3>
</div>
<div class="panel-body">
#foreach($categories as $cat)
<div class="col-lg-6">
<ul class="list-unstyled">
<li>News
</li>
</ul>
</div>
#endforeach
</div>
</div>
</div>
#if(count($articles))
#foreach($articles as $article)
<div class="panel panel-default col-md-8">
<div class="panel-heading">
<h3>{{ $article->title }}</h3>
<small>posted by {{ $article->author->name }} {{ $article->created_at }}</small>
</div>
<div class="panel-body">
{!! str_limit($article->body, 1000) !!}
<hr>
Read More
</div>
</div>
#endforeach
#else
<h1>No articles at the moment.</h1>
#endif
<br>
{!! $articles->render() !!}
#stop
You have two routes
Route::get('/home', 'ArticlesController#index');
Route::get('/home', 'CategoriesController#index');
Which means when you visit /home, only ArticlesController#index is fired, not both.
This means that the $categories variable used in the view is not populated because your index() method in CategoriesController is intended to do this but is never called.
Therefore you need to do something like this
Route::get('/home', 'HomeController#index');
public function index()
{
$categories = Category::all();
$articles = Article::all();
return view('/home', compact('articles', 'categories'));
}
try to use
return View::make('/home')->with(compact('categories'));
instead of
return view('/home', compact('categories'));
I insert this in the top of my #foreach
<?php
use App\models\PersonalContact;
$data = PersonalContact::all();
?>
or this before #foreach add #if statement like this
#if (count($data) > 0)
#foreach ($data as $bloginfo)
<tr role="row" class="odd">
<td class="sorting_1">{{$bloginfo->name}}</td>
<td>{{$bloginfo->description}}</td>
<td>{{$bloginfo->email}}</td>
<td>{{$bloginfo->phone}}</td>
<td><img src="{{url('images/'.$bloginfo->image)}}" alt="" width="100px" height="100px"></td>
<td><i class="fas fa-edit"></i>Add | <i class="fas fa-edit"></i>Edit | <a class="btn btn-app"><i class="fas fa-edit"></i>Delete</a></td>
</tr>
#endforeach
#endif
return view('/home', compact('categories'));
The problem was $categories on compact method, you must use like a string