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