In the Laravel documentation it says you can use this syntax for querying an object relation to get only the Posts that have at least one Comment:
$posts = Post::has('comments')->get();
I'm trying something similar where I want to fetch only objects that have at least one relation object. These are my two classes:
class Movie extends Eloquent {
protected $table = 'movie';
public function matches() {
return $this->hasMany("Match");
}
}
class Match extends Eloquent {
protected $table = 'match';
public function movie() {
return $this->belongsTo("Movie");
}
}
But when I call
$movies = Movie::has('matches')->get();
I get an empty collection. If I call
$movie = Movie::find(1)->matches()->get();
I do get the Match that relates to the Movie, so I know the relation is setup properly. I can't figure out what I'm doing wrong with the Movie::has method though.
I'm using the sqlite3 database included with a laravel project created with composer. This is the structure and data:
sqlite> .schema movie
CREATE TABLE "movie" ("id" integer not null primary key autoincrement, "title" varchar not null);
sqlite> .schema match
CREATE TABLE "match" ("id" integer not null primary key autoincrement, "movie_id" integer not null, "title" varchar not null, foreign key("movie_id") references "movie"("id"));
CREATE INDEX match_movie_id_index on "match" ("movie_id");
sqlite> select * from movie;
1|Test Movie
sqlite> select * from match;
1|1|Test Movie Match
with() function is used to eager load in Laravel. Unless of using 2 or more separate queries to fetch data from the database , we can use it with() method after the first command. It provides a better user experience as we do not have to wait for a longer period of time in fetching data from the database.
The only difference between hasOne and belongsTo is where the foreign key column is located. Let's say you have two entities: User and an Account. In short hasOne and belongsTo are inverses of one another - if one record belongTo the other, the other hasOne of the first.
To define a relationship, we need first to define the post() method in User model. In the post() method, we need to implement the hasOne() method that returns the result. Let's understand the one to one relationship through an example. First, we add the new column (user_id) in an existing table named as posts.
Using the hasOne relationship in Laravel First of all, we can explain a simple example of the use of hasOne: if an article has comments and we want to take one of them with the details of the article, we can use the hasOne relationship or a user can have a profile table.
This however works fine with the MySQL driver. When using SQLite as the database driver, has
returns an empty collection because the count gets wrapped in quotes. You may use the DB::raw
method to pass the count as a raw expression.
$posts = Post::has('comments', '>=', DB::raw(1))->get();
Related issues: #3353, #3435.
Edit: As patricus affirmed this issue was affecting only installations prior to Laravel 4.1.25. You don't need to use this workaround with newer versions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With