I am making an image gallery. Images have a many to many relationship with tags. Images also have a many to many relationship with catagories.
$images = $category->images;
$images = $tag->images;
public function images(Category $category = null)
{
$images = $category->images;
return view('pages.images.index', compact('images'));
}
Route model binding on this code works fine and I can do the same for tags but what I am looking to do is filter on category and then tag.
$images = $tag->category->images;
For example, if I had an image with the tag of "dog" and a category "brown" the query would only return all dogs that are brown.
Can I do this? Both tags and categories could have multiple options so I was hoping for a route like this...
mysite.com/images/{category}/{tag}
Thanks for any help or best direction to go.
You could use the build in laravel whereHas functionality
Image::whereHas('categories', function ($query) use ($category) {
$query->where('NAME', '=', $category);
})->whereHas('tags', function ($query) use ($tag) {
$query->where('NAME', '=', $tag);
})->get();
replace the NAME with the desired field you want to filter on.
You can declare the route just as you stated and then use them as parameters in the controller-method which is connected to the route.
So if you declare a route like Route::get('/register/{token1}/{token2}','RegisterController#confirm');then it is possible to get those wildcards in the method confirm just like normal parameters:
public function confirm(Request $request, $token1, $token2) {}
Related
I'm using laravel 7 and i've made an API to fetch data with a VUE component.
I want to filter restaurants based on their type of food. So if i query "pizza" i want to show only restaurants that make pizza.
getTypes is a many to many relation.
public function restaurants(Request $request)
{
$ricerca = $request->input('query');
$users = Restaurant::with(array('getTypes' => function ($query) use ($ricerca) {
$query->where('name', "LIKE", "%" . $ricerca . "%");
}))->get();
return response()->json($users);
}
I tried this, but this way i don't have access to other genres, if a restaurant make pizza and meat, i don't see meat anymore.
Any advice?
create a many to many relation with restaurant and pizza.
get an array which restaurant make pize,
$array = [];
or simple var
$array = $request('query');
and put this array in query.
$data = Project::whereHas('restorant_pizza', function ($q) use ($array) {
$q->whereIn('restorant_pizza.pizza_id', $array);
})->get();
I think this idea will help you
Hi all,
I'm building an application in Laravel, I want to get all item in an article category without using this Eloquent.
My code is
$info = Article::find($article_id);
$cate_info = $info->article_categories()->first();
if($cate_info){
$same = Article::with(['article_categories' => function ($query) use ($cate_info){
$query->where('articles_category.ID' ,$cate_info->ID);
}])->where('ID' ,'!=' ,$article_id)->get();
}
And I get all articles. How to solve.
Article and Article_Category is in many to many relationship.
Thanks.
If you want to have all the articles of a particular article_categories you can use whereHas method, now suppose you have name as a field column in categories table, so you can have something like this:
public function getData(Request $request)
{
$category = $request->categoryName;
// $category = 'ABC Category';
$articles = Article::whereHas('article_categories', function($query) use($category) {
$query->where('name', 'like', '%'.$category.'%');
})->get();
return response()->json(['articles' => $articles], 200);
}
Hope this helps.
i have a question regarding whereHas in Laravel
public function index($category = null) {
$videos = Video::with(['content', 'content.categories' => function($query) use ($category) {
return $query->where('name', $category);
}])->paginate(10);
return view('frontend.videos')->with('videos', $videos);
}
Take that method, i have a Video model, the Video Model has a 1-to-1 relationship with the Content Model which in turn has a many-to-many relationship with the Categories Model
I need to get all videos where the which have a content with a category where the name matches whatever has been searched, is this possible? if so how?
I've figured it out, the final solution looks something like this, the eager loading will get me the Categories Model in the Subquery and i can simply use that and check against it.
$videos = Video::whereHas('content.categories', function($categories) use ($category) {
$categories->where('name', $category);
})->paginate(10);
I've followed this code structure for a multi-level category system on Laravel 5:
This works fine. But I'm trying to put together a query to return posts in a category (by slug) but also the children categories underneath.
So heres what I've got at the moment:
$posts = Post::with('images')->whereHas('categories', function($q) use ($slug)
{
$q->where('slug', '=', $slug);
})->orderBy('created_at', 'DESC')->paginate(10);
This returns everything under the category that matches the slug passed through, but not the children. Any suggestions?
Thanks.
If you have only one depth of subcategories, the following solution is what I would use.
<?php
$category = Category::where('slug',$slug)->first();
$posts = Post::with('images')->whereHas('categories', function($q) use ($category)
{
$q->where(function($q) use ($category) {
$q->where('id', $category->id)->orWhere('parent_id',$category->id);
});
})->orderBy('created_at', 'DESC')->paginate(10);
In your code, you only fetched the Posts and their images. If I understood correctly, what you want to fetch are the Posts, their images, the category their in and childCategories (this is a made up name) of that category. You just need to add category.childCategories in your with method like so:
$posts = Post::with('images', 'categories.childCategories')->whereHas('categories', function($q) use ($slug)
{
$q->where('slug', '=', $slug);
})->orderBy('created_at', 'DESC')->paginate(10);
Remember to change the childCategories name to whatever the child category relationship name in your Category model.
I have an eloquent object Performer that has Albums and Albums have Images
Here is setup:
Model Performer->albums():
public function albums()
{
return $this->belongsToMany('Album','performer_albums','performer_id','album_id');
}
Model Album->images()
public function images()
{
return $this->belongsToMany('Image','album_images','album_id','image_id')->withPivot(['type','size']);
}
I have performer object stored as such:
$performer = Performer::where...->first();
Now I need to get Performer's Albums with images where size is 'large'
So to avoid nesting queries, can I use with()?
I tried
$performer->albums()
->with('images')
->wherePivot('size','large')
->get();
But laravel tells me it's trying to use wherePivot for Performer-Album relationship (M-2-M)
PS. I am also aware that I can do this,
$performer = Performer::with('albums')
->with('albums.images')
->.....-conditions for additional fields in album_images....
->get();
but question remains the same.
You need eager load constraints:
$performer->albums()
->with(['images' => function ($q) {
$q->wherePivot('size','large');
}])
->get();
And btw, no, you can't do this:
Performer::with('albums')
->with('albums.images')
->.....-conditions for additional fields in album_images....
->get();
instead you could do:
Performer::with(['albums.images' => function ($q) {
$q-> .....-conditions for additional fields in album_images....
}])->get();