Sorting database results by category - php

I have a table in my database with the fields 'Category' and 'Title'. I have multiple records with the same category but other titles. What I'd like to do is print the category once on my page then show the all the titles with the same category.
So Something like this:
Category 1
Title 3(=newest)
Title 2
Title 1(=olddest)
Category 2
Title 1
Category 3
Title 3(=newest)
Title 2
Title 1(=olddest)
Im using the Laravel 4 framework with Eloquent. So I'm getting the result back as an JSON object.
What I have at the moment:
view
#foreach($photos as $photo)
{{$photo->Category}}
#foreach($photo as $category)
{{ $photo->Title }}
#endforeach
#endforeach
Controller
$photos = Photo::orderBy('Date')->get(); // Sort by so that newest photos come first per category
return View::make('myView')->with('photos', $photos);
When looking a bit further I came on to array_add helper but I'm not sure if I can use it and how I really should use it.
Can someone help me achieve the result I need?

You can do something like:
$photos = Photo::orderBy('category')->orderBy('created_at', 'desc')->get();
return View::make('myView')->with('photos', $photos);
Then
<?php $category = ''; ?>
#foreach($photos as $photo)
#if($category != $photo->Category)
{{$photo->Category}}
<?php $category = $photo->Category; ?>
#endif
{{ $photo->Title }}
#endforeach

Related

create counter for blog categories in laravel

I´m traying to create one counter for my blog´s categories. This should appear to the right name side of my category . i´m using my model with variable appends, that after i will use in my blade for show my result in one span. But i don´t know very well how i can use count in my Model. I´m doing this in my model Blog.
my variable appends contain:
protected $appends = [
'custom_fields',
'has_media',
'restaurant',
'blog_category',
'viewer',
'postCounter',
];
i´m traying this:
return $this->blogs()->count();
i have a relation between blog and blog_category with:
public function blogCategory()
{
return $this->belongsTo(\App\Models\BlogCategory::class, 'blog_category_id', 'id');
}
i want to do in my view appear for example:
innovation (2)
in my view i´m doing this:
#foreach($categories as $category)
<li>{{ trans('web.blog_category_'.$category->name) }}<span>{{$category->postCounter}}</span></li>
#endforeach
but always returned me 0, and i have post with categories
updated
With laravel relationship withCount you can do this easily. If you want to count the number of results from a relationship without actually loading them you may use the withCount method, which will place a {relation}_count column on your resulting models.
add withCount method to your query
$categories = Category::withCount('blogCategory')->get();
You can access the count in your foreach loop
// $category->blogCategory_count
#foreach($categories as $category)
<li>
<a href="{{ url('blogs/'.$category->name) }}">
{{trans('web.blog_category_'.$category->name) }}
</a>
<span>{{$category->blogCategory_count}}</span>
</li>
#endforeach

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

Laravel: Eloquent relationships with *where* on parent table instead of *find*

I have a table posts and posts_contents.
And I want to get a content from one post only if that post has display = 1.
(I need two separate tables because of language support)
posts:
id user_id display
1 2 0
2 2 1
3 2 0
4 2 1
posts_contents
id post_id lang_id name description
1 1 1 Hello World
2 2 1 Here Is What I wanna show!
3 3 1 Don't Show the others
4 4 1 Hey Display that one too
So in laravel I use eloquent relationships, but I just don't understand how to use it in that particular case. In the documentation I found only cases such as:
$p = App\Posts::find(1)->contents;
Which works great, however what I want is something like this:
$p = App\Posts::where('display',1)->contents;
But it doesn't work... So question is: what is the right way to do so?
Any help is appreciated, Thanks!
Update
I need to get multiple posts at once, not just one.
You want to use find() method like this:
$post = App\Posts::where('display', 1)->find($postId)->contents;
Then in a view for one-to-one relationship:
{{ $post->description }}
For one-to-many:
#foreach ($post->contents as $content)
{{ $content->description }}
#endforeach
If you want to load multiple posts with contents only for one language, use filtering by a language. Use with() to eager load contents:
$posts = App\Posts::where('display', 1)
->with(['contents' => function($q) use($langId) {
$q->where('lang_id', $langId);
}])
->get();
Then in a view for one-to-one:
#foreach ($posts as $post)
{{ $post->contents->description }}
#endforeach
For one-to-many:
#foreach ($posts as $post)
#foreach ($post->contents as $content)
{{ $content->description }}
#endforeach
#endforeach
You can read about the difference between find() and get() methods here.
App\Posts::where will return a collection. So if you only want 1 result you should use App\Posts::where('display',1)->first()->contents
You need to call the first method before you call any relationship:
$p = App\Posts::where('display', 1)->first()->contents;
Or, if you want to fetch a collection of posts, you can:
$posts = App\Posts::where('display', 1)->get();
$posts->each(function ($post) {
$post->contents;
});
Otherwise, you will just have a Query Builder object, without the actual results you want.

Puling a specific item from a foreach loop in Laravel

I have a project with 2 tables(Categories and products). They have One to Many relationship. Many products have one Category. Am pulling products of a specific category(together with its price and description) and storing in one variable so that I can use in the view. In the view am having a foreach loop whereby I want to display each row from the products table of a specific category(a product with its price and description) but it fails,,please assist?
FrontController
public function itemOne() {
//Get all phones(Have category of 1 ) in categories table
$mobile = Category::find(1)
->products()
->whereNotNull("image")
->whereNotNull("name")
->whereNotNull("description")
->whereNotNull("price")
->whereNotNull("category_id")->get();
return view('front.products')->withItems($mobile);
}
Products.blade.php
#foreach($items as $item)
<img src="{{ asset('images/'.$item->image) }}">
<br> {{ $item->name}} <br> {{ $item->price }}
#endforeach
public function itemOne() {
//Get all phones(Have category of 1 ) in categories table
$mobile = Category::find(1)
->products()
->whereNotNull("image")
->whereNotNull("name")
->whereNotNull("description")
->whereNotNull("price")
->whereNotNull("category_id")->get();
return view('front.products',compact('items'));
}
use compact in return

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