How to Limit Related Model from Parent Model in Laravel,
I have Category and News Model, I want to fetch all categories and each category with 3 latest news.
Please try this. You will get all categories that as minimum one news model related
$news = Category::whereHas('news',function($query){
$query->orderBy('created_at', 'desc')->take(3);
})->get();
Related
I am using Eloquent ORM and I have Book model which connect to BookCategory model and BookCategory connect to Category. The l'm problem facing is how to include data from third table in eloquent relationships?
Book
id
name
Category
id
name
type
BookCategory
id
book_id
category_id
Lets say for example you want to get all the books of a certain category:
assuming your pivot table name is Book_Category
in your Category model:
public function books()
{
return $this->belongsToMany('App\Models\Book', 'Book_Category', 'category_id', 'book_id');
}
and you can eager load category books like :
$categories = Category::get()->load('books');
//OR
$categories = Category::with('books')->get();
If I understand you right, you are looking for the pivot attribute. This makes additional columns of the intermediate table available.
https://laravel.com/docs/8.x/eloquent-relationships#retrieving-intermediate-table-columns
I'm creating an application where user can have one speciality.
User can in backend create posts assigned to his speciality, and has to associate it to one category.
I'm wondering how to manage categories when a doctor and an lawyer can use the same category, and not display all posts for all specialities where category is the same.
So actually:
user has one speciality
speciality has many categories
category belongs to many specialities
I think i have to create a pivot like category_speciality, as explained in Laravel doc for many to many relationships.
But then i can't figure how to use speciality an category to build dedicated queries and urls.
Any help will be great !
For URL's you could do something like:
web.php
Route::get('/articles/{category}/{specialty}', [
'uses' => 'PostController#index',
'as' => 'posts.index'
]);
where {category} and {specialty} are a unique slug field of their title. This would result in e.g. example.com/articles/deep-sea/fishing.
PostController
public function index($categorySlug, $specialtySlug)
{
$specialty = Specialty::where('slug', '=', '$specialtySlug')->first();
$posts = Posts::where('specialty_id', '=', $specialty->id)->get();
return view('posts.index', compact('posts'));
}
That is assuming you have specialty_id in Posts table and you have correct relationships set up.
This is just an example.
I have simple Laravel relations between Category and Article models. I need to return the list of all categories with related articles.
Test data: 3 categories and 15 articles (5 articles per category).
Expected result: collection of all categories with 2 latest related articles for each category
By default case I should use something like:
Category::with('articles')->get();
It's ok - I got 3 categories. Each category has "relations" with 5 related records.
Next, I tried:
Category::with(['articles' => function ($query) {
$query->orderBy('created_at', 'desc');
$query->limit(2);
}])->get();
But this is the wrong way. This construction in "raw" query looks like:
select * from `articles` where `articles`.`category_id` in (select id from categories) limit 2
So it's returned me a wrong result as expected...
I need all categories and latest 2 articles for each category (3 * 2 = 6 records) this query returned just 2 records
There is no native support for this in Laravel.
I created a package for it: https://github.com/staudenmeir/eloquent-eager-limit
Use the HasEagerLimit trait in both the parent and the related model.
class Category extends Model {
use \Staudenmeir\EloquentEagerLimit\HasEagerLimit;
}
class Article extends Model {
use \Staudenmeir\EloquentEagerLimit\HasEagerLimit;
}
Then you can apply ->limit(2) to your relationship.
Hi I have two models Category,News, i want to retrieve categories with 3 latest news in each category, News models have created_at column for date when it created.
Can i achieve this in laravel
try this way,
$news = Category::whereHas('news',function($query){
$query->orderBy('created_at', 'desc')->take(3);
})->get();
I have 3 tables in my database. users, books and book_user.
book_user is the pivot table that manages the many-to-many relationship between users and books.
I have the Laravel relationship correctly setup and i am able to display all the books associated to a user using the following in Laravel.
$user = Auth::user();
return response()->json($user->books()->get());
Now i am trying to display all the books that are NOT associated to a particular user. I have been struggling for quite some time and cant seem to find anything that works. Any tips would be greatly appreciated.
If the book is not related to the user then you should not try to retrieve it via the user. Instead use the book model:
Book::whereDoesntHave('users', function ($q) {
$q->where('id', \Auth::id());
})->get();
If you want all books not belonging to any user at all, instead of all books not belonging to a particular user, you could do something like :
$books = Book::whereDoesntExists(function($query) {
$query->select('book_user.id_book')
->from('book_user')
->where('book_user.id_book', '=', 'books.id');
// Assuming that the columns on the pivot table are 'id_book' and 'id_user' and the primary key is 'id' in the 'books' table.
});
Here you fetch all books that haven't their id in the book_user pivot table's id_book column. So basically, all books not belonging to any user.