PHP Laravel how to sort by using two tables - php

I have two MySQL tables in my Laravel-application, one called categories and the other called employees. The structure of the categories-table is:
id
category
order
and the employees table also has columns called:
id
category
order
So, lets say I have categories like: Consultants, Programmers and Administration and when I create an Employee in the backend I can assign the new employee to one of these categories. Now in the frontend of my Laravel-application I want the Employees displayed by the categories, and also the categories by order they are given. Let's say Consultants has order of 2, Programmers order of 1 and Administration order of 3.
Right now my controller looks like this:
use App\Employee;
class EmployeesController extends Controller
{
public function index()
{
$employees = Employee::all()->groupBy('category');
return view('app.employee.index', compact('employees'));
}
}
and my blade view file:
#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('order') as $worker)
// content of the employee
#endforeach
</div>
#endforeach
This sorts the employees correctly by simply using the categories of the Employees-table but with this I'm not able to sort by categories like I want to as described above.
So, can someone help me out?
EDIT
As an example I want the output look like this:
Programmers (since this category has order of 1)
// employees with category "programmers" here
Consultants (2)
// employees with category "consultants" here
Administration (3)
// employees with category "administration" here

To me you column definitions are a bit confusing, may I suggest a change to your columns:
Table 'categories'
------------------
id
name
order
Table 'employees'
-----------------
id
category_id
Add a foreign key to the employees table:
$table->foreign('category_id')->references('id')->on('categories')
And then your models could be mapped to each other with relationship methods:
class Employee extends Model
{
public function category()
{
return $this->belongsTo(Category::class);
}
}
class Category extends Model
{
public function employees()
{
return $this->hasMany(Employee::class);
}
}
So with this in place we can simply query the database from the controller by:
use App\Category;
class EmployeesController extends Controller
{
public function index()
{
$categories = Category::orderBy('order')->get();
return view('app.employee.index', compact('categories'));
}
}
and display the results in your blade view:
#foreach ($categories as $category)
<div class="col text-center mb-6">
<h2>{{ $category->name }}</h2>
</div>
<div class="row px-4 px-xl-10">
#foreach($category->employees as $employee)
// content of the employee
#endforeach
</div>
#endforeach

I think you must change your query to :
$categories = Category::with(['employees'=>function($q){
$q->orderBy('order');
}])->orderBy('order')->get();

Related

Laravel filter products by category name

I use Bagisto E-Commerce platform (build laravel + vue) and I have created package where I want list categories and filter products by category name.
I don't know how exactly filter products by one view and in bagisto they list products by category page e.g. example.com/category-name
I try to use this example, but can't get it working, because I don't know where I get class "Products" and function AllProducts.
Can someone guide me in the right direction of how I could get this to work correctly?
This is what I'm trying to do: https://codepen.io/mrsingleton/pen/aYVBvV
My code in products view:
$categories = [];
foreach (app('Webkul\Category\Repositories\CategoryRepository')->getVisibleCategoryTree(core()->getCurrentChannel()->root_category_id) as $category) {
array_push($categories, $category);
}
?>
#if (count($categories))
<div class="list-container" style="text-align:center;margin-top: 50px;">
<ul class="list-group">
#foreach ($categories as $key => $category)
<li> {{ $category->name }} </li>
#endforeach
</ul>
</div>
#endif
In Bagisto, Laravel translatable has been used. Thats why you need to use the different approaach to achieve this. As directly by name, you won't be able to achive because the rest columns are on different tables i.e. category_translations.
If you check the Categories model in namespace i.e. Webkul\Category\Models\Category, it is already linked to the Product model. So you can directly chain it to fetch all associated products.
/* fetch category by name */
$categoryName = 'Category 1';
$category = Category::whereHas(
'translations',
function ($query) use ($categoryName) {
$query->where('name', $categoryName);
}
)->first();
$relatedProducts = $category->products;
/* rest is your operations */

how to show multiple category name from checkbox value in eloquent relationship laravel

firstly, i'm a newbie in laravel.
i insert a array data as a string in server.
column name "category_id" & value like "1,2,3,4".
Now i just want to show these id's category name with eloquent relationship.
Using laravel latest version.
view page
{{$category_var = explode(',',$post->category_id)}}
#foreach($category_var as $category)
#if($category)
<span class="badge mb-3">{{$post->category->category}} </span>
#endif
#endforeach
class page
public function category(){
return $this-> belongsTo(Category::class);
}
Controller page
public function index()
{
$posts = post::paginate(9);
return view('pages/home',compact('posts'));
}
anything else need just ask me.
Thanks
just use the benefits of models and relation like below:
#foreach($posts as $post)
#foreach($post->category as $category)
<span class="badge mb-3">
{{ $category->(what ever you want from category model) }}
</span>
#endforeach
#endforeach

stuck at DB query, laravel

I need your help...
Can't figure out where the problem is.
I am trying to show all products of a subcategory.Sometimes it shows the first or the last record. Then it repeats many times the same record( as the cycle is).
category: id, name, visible
products:id, name,
category_products:id, id_product, id_category
Route::get('navigation/{id}',function($id){
$prods= \App\Products_to_Categories::where('id_category',$id)->get();
$products=array();
foreach ($prods as $prod)
{
$products[] = \App\Products::find($prod->id_product)->
where('visible','yes')
-> where('delete','no')->first();
}
return view('subcategories.order_products',
['products'=>$products ]);}
View blade
<div class="col-md-6 col-md-offset-1">
<ul id="sortable">
#foreach($products as $product)
<li class="ui-state-default" id="{{ $product->id}}"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span> {{$product->name}}</li>
#endforeach
</ul>
</div>
It looks like products and categories are related through the join table category_products, so you can setup a belongsToMany() relationship and query from Category to Product without looping over the join table.
https://laravel.com/docs/5.7/eloquent-relationships#many-to-many
Category model:
public function products()
{
return $this->belongsToMany(\App\Products::class, 'category_products', 'id_category', 'id_product');
}
Products model:
public function categories()
{
return $this->belongsToMany(\App\Category::class, 'category_products', 'id_product', 'id_category');
}
Controller code:
$category = Category::find($id);
$products = $category->products()
->where('visible', 'yes')
->where('delete', 'no')
// ->inRandomOrder() // un-comment this if you want results in random order
->get();
Try this in Laravel >= 5.2: :
$prods= \App\Products_to_Categories::where('id_category',$id)->get();
$products=array();
$whereIn = array();
foreach ($prods as $prod)
{
$whereIn[] = $prod->id_product;
}
$products[] = \App\Products::find($prod->id_product)
->where('visible','yes')
-> where('delete','no')
->whereIn('id', $whereIn)
->orderByRaw('RAND()')
->get();
This will give you the list of products of a specific category in random order.
Laravel 4.2.7 - 5.1:
User::orderByRaw("RAND()")->get();
Laravel 4.0 - 4.2.6:
User::orderBy(DB::raw('RAND()'))->get();
Laravel 3:
User::order_by(DB::raw('RAND()'))->get();
source :
Laravel - Eloquent or Fluent random row

Select from two tables and show everything in laravel

I want to show on the page data from two tables but with a little condition and I can't figured it out how to make it in Laravel 4.
So I have table categories and table products. Currently I show all categories on the page and there is no problem. The problem is now there will have products without category and I want to loop them also on the page.
When admin create product he choose category but if doesn't choose any it will save 1 default in database products column single_product.
This is Product model
public function categories()
{
return $this->hasMany('Categories', 'category_id');
}
And Categories model
public function products()
{
return $this->hasMany('Product', 'category_id');
}
public function lowestProduct() {
return $this->products()->selectRaw('*, max(price) as aggregate')
->groupBy('products.product_id')->orderBy('aggregate');
}
And this is the view
<div class="col-xs-8 text-left" style="margin-top: 9px">
#if($category['no_category'] == 0)
Price: <strong>{{ $category->products()->min('price') }} $</strong>
#else
Price from: <strong>{{ $category->products()->min('price') }} $</strong>
#endif
</div>
How to select single column from products table and show them on the page along with categories name?
Then there is no category. Avoid your category class and just fetch directly from the product class. Create a function like:
public function singleItems()
{
return $this->where("single_product", 1)->get();
}
Then you foreach the result out. Please check this for further information:
https://laravel.com/docs/5.1/eloquent-collections
I know above is for another version of laravel than yours, but I think it should work. Else tell me, then I will look further.

Laravel 5 - Count the number of posts that are assigned to each category in a blog

I was wondering what the cleanest way was to count the number of posts that are connected to a category in my blog.
Here is how the table relationship is set up.
What I have is a hasMany relationship from the Category to the Post models like this:
In Categories Model
public function blog_posts()
{
return $this->hasMany('App\Http\Models\Blog_Post', 'category_id');
}
And in the Blog_Post Model
public function blog_categories()
{
return $this->belongsTo('App\Http\Models\BlogCategories', 'category_id');
}
In effect all I want to do is be able to return to my view the total number of posts that each category has as shown below. Where x is the number of posts within each category.
cat1 (x)
cat2 (x)
cat3 (x)
It's not hard to count I know however as I only want a count I do not want to also retrieve the records as they are not required and I also do not want to create more queries than is necessary.
I have not completed the view as yet but probably a start would be to pass through the categories in a loop to display each and add the count at the same time?
#foreach ($categories as $category)
{!! $category->name !!} - {!! Count of posts here !!}
#endforeach
Hopefully that is clear(ish)!
Eager load the relation in your controller:
public function index()
{
$categories = Category::with('blog_posts')->get();
return view('categories.index', compact('categories'));
}
You can then use the count() method on the blog_posts relation when looping over categories in your view:
#foreach ($categories as $category)
<li>{{ $category->name }} ({{ $category->blog_posts->count() }})</li>
#endforeach
EDIT: Since Laravel 5.3, you can use withCount() to load counts of relations, i.e.
$categories = Category::withCount('blog_posts')->get();
This will make the count available via a property:
foreach ($categories as $category) {
$blog_posts_count = $category->blog_posts_count;
}
The nicest way to do it with eager loading support I know is to create a separate relation with the post count. Check this out:
public function blog_posts_count() {
return $this->hasOne('App\Http\Models\Blog_Post', 'category_id')
->selectRaw('category_id, count(*) as aggregate')
->groupBy('category_id');
}
public function getBlogPostsCountAttribute() {
if(!array_key_exists('blog_posts_count', $this->relations))
$this->load('blog_posts_count');
$related = $this->getRelation('blog_posts_count');
return $related ? (int) $related->aggregate : 0;
}
Usage is simple:
{{ $category->blog_posts_count }}

Categories