My model Book
public function author()
{
return $this->belongsTo('Author');
}
I want to get all books which have authors that have column is_published == 1.
So I have tried this:
Book::with(['author' => function($query){
$query->where('is_published', '=', '1');
}]);
This works but really only gets me Books where SOME books have author model attached and some don't!
So, I tried this:
Book::with(['author' => function($query){
$query->where('is_published', '=', '1');
}])->has('author');
But I get this error:
Has method invalid on "belongsTo" relations
How can I be sure that my constraint on the foreign table reduces my final data set without having to loop through my data and check for the existence of the author? Thanks.
Book::whereHas('author' , function($query){
$query->where('is_published', '=', '1');
})->get();
Use this it worked for me.
The problem is that eager loading does not utilise the SQL JOIN (that I know of) if you head over to http://laravel.com/docs/eloquent#eager-loading you will see the example:
select * from books
select * from authors where id in (1, 2, 3, 4, 5, ...)
This does not allow the constraint of the author existing.
To do this it requires a JOIN if you want to do it efficiently (I assume so hence the eager loading).
Relevant Laravel Documentation: http://laravel.com/docs/queries#joins
Replace has('author') with whereNotNull('author_id').
Related
I have a query which selects some relationships and groups by the accommodations to remove the duplicate accommodations. But now when I want to load the discount realtion on rooms, it doesn't work because I select only the accommodation_id.
Here is my code:
$data = AccommodationRoom::with('accommodation.city', 'accommodation.accommodationFacilities', 'accommodation.gallery','discount')
->select('accommodation_id')
->whereHas('roomCapacityHistory', function ($query) use ($from_date, $to_date) {
$query->whereDate('from_date', '<=', $from_date);
$query->whereDate('to_date', '>=', $to_date);
})
->whereHas('accommodation', function ($query) use ($searchCity) {
$query->where('city_id', $searchCity);
})
->groupBy('accommodation_id')
->get();
Now if I add the id to the select it would be fine, but my groupBy doesn't work and gives me an error in this case. So I need a solution to get all my accomodations with the listed relations.
As you are looking for all your accommodations with some related models, you should actually select from your Accommodation model. This will work out of the box for the first three relations but will require some tweaking for the discount relation. The simpliest solution is to create a HasManyThrough relation on the Accommodation model:
class Accommodation extends Model
{
public function discounts()
{
return $this->hasManyThrough(Discount::class, AccommodationRoom::class);
}
}
Note: this expects your models to use foreign key columns named by convention; for different names you will need to pass the custom foreign key names as additional parameters according to the documentation.
With this relation set up, you can then use a query like the following:
$data = Accommodation::with('city', 'accommodationFacilities', 'gallery', 'discounts')
->whereHas('accommodationRooms.roomCapacityHistory', function ($query) use ($from_date, $to_date) {
$query->whereDate('from_date', '<=', $from_date);
$query->whereDate('to_date', '>=', $to_date);
})
->where('city_id', $searchCity)
->get();
Further explanation as asked for in the comments:
HasManyThrough builds a virtual relation between two models using a third model in between. Imagine you have Post, Comment and Like as models. One Post can have many Comments and one Comment can have many Likes:
has many has many
Post -----------------> Comment -----------------> Like
1 n 1 n
In this case we also know that one Post can have many Likes. And this is exactly the knowledge we utilize when using HasManyThrough. We simply tell Eloquent that a Post has many Likes, connected by the Posts in between:
has many has many
Post -----------------> Comment -----------------> Like
1 n 1 n
has many through Comment
Post ------------------------------------------------> Like
1 n
I came accros a problem with laravel's ORM, eloquent and found no solution yet.
I have some tables as follows
Team
- id
- name
User
- id
- name
- role
- team_id
Student_Info
- id
- user_id
- data1
- data2
- etc ...
Project
- id
- student_id
- name
Now, I want to query all projects a certain team, where team = 'some team'
Now the thing here is, without an ORM, it's simple, I would have done multiple joins in raw SQL.
However, because all these tables have a common column "name" I will have to alias all this stuff, which is really boring
With eloquent I can't find a way to do this query using "has many through" because it only allows on intermediate and I can't do a raw SQL as the alis thing is really a pain in the ass and as it would be very difficult to map the result to laravel's Models
There is no native relationship for this case.
I created a HasManyThrough relationship with unlimited levels: Repository on GitHub
After the installation, you can use it like this:
class Team extends Model {
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
public function projects() {
return $this->hasManyDeep(Project::class, [User::class, StudentInfo::class],
[null, null, 'student_id']);
}
}
$projects = Team::where('name', 'some team')->first()->projects;
Try with relationship existence. This assumes you have all relationships properly defined
$projects = Project::whereHas('students.user.team', function ($query) {
$query->where('name', '=', 'some team');
})->get();
That's 3 levels of nesting. Never tested. However, if you already define a Project-User relationship via hasManyThrough() you can shorten it to 2 levels only.
$projects = Project::whereHas('user.team', function ($query) {
$query->where('name', '=', 'some team');
})->get();
Those will give you the data for projects only. If you also want the the intermediate data, use eager loading instead with with(). Just replace whereHas() by with().
$projects = Project::with('user.team', function ($query) {
$query->where('name', '=', 'some team');
})->get();
I'm trying to get "reviews" from my database where the user is from MX (for example) so, I have this:
Review::with(array('user' => function($query)
{
$query->where('country_code', '=', 'MX');
}))
->with('user.country','pictures')
->where('shop_id',Input::get('id'))
->orderBy('id','DESC'))
->get()
->toArray()
But seems to be like where('country_code', '=', 'MX') is not taken into account because retrieve all reviews and I just want the reviews written by the user from MX.
user and picture inside with are functions within my User model
country_code is a field from users table
The goal is: Just get the reviews written by a user from the country specified, and was testing something like this:
Review::where('shop_id',Input::get('id'))
->with('user.country','pictures')
->where('users.country_code','MX')
->orderBy('id','DESC'))
->get()
->toArray()
But is not working as well because says: Unknow column users.country_code in where....
You want to do a query that filters based on the relation. Eager loading constraints only constrain the records that come with your main result set.
Review::whereHas('users', function ($q)
{
$q->where('country_code', 'MX');
})
->with('user','pictures')
->where('shop_id',Input::get('id'))
->orderBy('id','DESC'))
->get();
The whereHas is saying ... give me only Reviews that have a User with country_code=MX.
Reference
Laravel Docs - Eloquent - Querying Relations
If you want reviews written by a certain user or group of users you don't want to use eager loading. Try this:
Review::with(array('user', 'pictures'))
->join('users', 'reviews.user_id', '=', 'users.id)
->where('users.country_code', '=', 'MX')
->where('reviews.shop_id', Input::get('id'))
->orderBy('reviews.id', 'DESC'))
->get();
Using Laravel 4 Eloquent ORM, any idea why the following is pulling every row instead of just the one I'm asking for (book_id=3)?
if ($Book = Book::find(3)) {
return (Book::with(array('chapters.pages' => function($query)
{
$query->where('book_id', '=', 3 ); // hard-coded id for illustration purposes
})
)->get()->toJson());
}
The output I'm getting is every single book with every chapter and every page. I only want to pull that single book with chapters and pages.
According to the documentation I should be able to add a constraint. Seems like I'm following the docs pretty closely. Thanks in advance.
I think the problem is you are asking for Book::with() - but you have not put a constraint on the Book() itself. So you need to do Book::with()->where()
Does this work
Book::with(array('chapters.pages' => function($query)
{
$query->where('book_id', '=', 3 ); // hard-coded id for illustration purposes
})
)->where('id', '=', '3')->get()->toJson());
note the ->where() - which is putting the id constraint on the query.
Edit: if you have correctly defined relationships (such as HasMany() etc) - then this should work:
$books = Book::with('chapters.pages')->where('id', '=', '3')->get();
Because the relationship with mean it only gets the 'chapters/pages' for the book with id of 3 by defintion.
Using Laravel 4 I have the following models and relations: Event which hasMany Record which hasMany Item. What I would like to do is something like this
Item::where('events.event_type_id', 2)->paginate(50);
This of cause doesn't work as Eloquent doesn't JOIN the models together when retrieving the records. So how do I go about this without just writing the SQL myself (which I would like to avoid as I want to use pagination).
What you want is eager loading.
It works like this if you want to specify additional constraints:
Item::with(array('events' => function($query) {
return $query->where('event_type_id', 2);
}))->paginate(50);
There is a pull request pending here https://github.com/laravel/framework/pull/1951.
This will allow you to use a constraint on the has() method, something like this:
$results = Foo::has(array('bars' => function($query)
{
$query->where('title', 'LIKE', '%baz%');
}))
->with('bars')
->get();
The idea being you only return Foos that have related Bars that contain the string 'baz' in its title column.
It's also discussed here: https://github.com/laravel/framework/issues/1166. Hopefully it will be merged in soon. Works fine for me when I update my local copy of the Builder class with the updated code in the pull request.