Laravel Querying Relations Model::has('relation') doesn't work

谁都会走 提交于 2019-12-12 10:56:24

问题


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.



来源:https://stackoverflow.com/questions/20714559/laravel-querying-relations-modelhasrelation-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!