【发布时间】:2019-09-27 05:11:56
【问题描述】:
我想要的是向 eloquent 模型添加方法,以便我可以将它们链接起来,例如:
class MovieResolver
{
public function getMoviesFeaturingToday(array $args)
{
// Movie is an Eloquent model
$movie = (new Movie())
->getMoviesFeaturingTodayOnTheater($args['movieTheaterId'])
->getBySessionCategory($args['sessioncategory']);
// And keep doing some operations if necessary, like the code below.
// I cannot call the get() method unless I finish my operations.
return $movie->whereDate('debut', '<=', Carbon::today())
->orderBy('debut', 'desc')
->get();
}
}
但是将这些方法添加到模型中:
class Movie extends Model
{
public function getMoviesFeaturingTodayOnTheater($theaterId)
{
return $this->whereHas(
'sessions.entries.movieTheaterRoom',
function ($query) use ($theaterId) {
$query->where('movie_theater_id', $theaterId);
}
);
}
public function getBySessionCategory($sessionCategory)
{
return $this->whereHas(
);
}
}
导致以下错误:
调用未定义的方法 Illuminate\Database\Eloquent\Builder::getMoviesFeaturingTodayOnTheater()
但是为什么呢?我做错了什么?
【问题讨论】:
标签: laravel eloquent laravel-query-builder