Laravel Querying Relations Model::has('relatio

2019-06-20 16:54发布

问题:

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

回答1:

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.