我有以下 Eloquent 实体:
- 电影
- 电影 session
- 电影院
- 电影院
表结构:
Schema::create('movies', function (Blueprint $table) {
$table->increments('id');
});
Schema::create('movie_theaters', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->index();
$table->string('slug')->index()->unique();
$table->string('description')->index()->nullable();
$table->longText('content')->nullable();
$table->unsignedInteger('picture_id')->nullable();
$table->foreign('picture_id')
->references('id')
->on('media');
$table->boolean('activated')->default(true);
$table->timestamps();
});
Schema::create('movie_theater_rooms', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->index();
$table->string('description')->index()->nullable();
$table->longText('content')->nullable();
$table->boolean('activated')->default(true);
$table->unsignedInteger('movie_theater_id');
$table->foreign('movie_theater_id')
->references('id')
->on('movie_theaters')
->onDelete('cascade');
$table->unsignedInteger('picture_id')->nullable();
$table->foreign('picture_id')
->references('id')
->on('media');
$table->timestamps();
});
Schema::create('movie_sessions', function (Blueprint $table) {
$table->increments('id');
$table->dateTime('starts_in');
$table->dateTime('ends_in');
$table->unsignedInteger('movie_theater_room_id');
$table->foreign('movie_theater_room_id')
->references('id')
->on('movie_theater_rooms')
->onDelete('cascade');
$table->unsignedInteger('movie_id');
$table->foreign('movie_id')
->references('id')
->on('movies')
->onDelete('cascade');
$table->boolean('activated')->default(true);
$table->timestamps();
});
其中一个 MovieTheater 有许多 MovieTheaterRoom,每个 MovieTheaterRoom 有许多 MovieSession 并且每个 MovieSession 属于电影。
我正在尝试的是通过 $movieTheaterId
获取今天在 MovieTheater 上播放的所有 Movie,但是由于这是一个长期的关系,我无法检索这样的集合。
这是我试过的:
public function scopeGetMoviesShowingTodayOnMovieTheater($movieTheaterId)
{
return Movie::whereHas('sessions', function ($query) use ($movieTheaterId) {
// $query->whereDate('starts_in', Carbon::today());
$query->whereHas('movieTheaterRoom', function ($query) use ($movieTheaterId) {
$query->where('movie_theater_id', $movieTheaterId);
});
});
}
当调用 App\Models\Movie::getMoviesShowingTodayOnMovieTheater(1)->get()
时,这是我得到的:
PHP Recoverable fatal error: Object of class Illuminate/Database/Eloquent/Builder could not be converted to string in .../vendor/illuminate/support/Str.php on line 338
我也试过像这样使用这个包staudenmeir/eloquent-has-many-deep
:
class MovieTheater extends Model {
use HasRelationships;
public function movies()
{
return $this->hasManyDeep(Movie::class, [MovieSession::class, MovieTheaterRoom::class]);
}
}
并且在调用 App\Models\MovieTheater::find(1)->movies()->get()
时
这是输出:
Illuminate/Database/QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'movie_sessions.movie_theater_id' in 'field list' (SQL: select
movies
.*,movie_sessions
.movie_theater_id
frommovies
inner joinmovie_theater_rooms
onmovie_theater_rooms
.id
=movies
.movie_theater_room_id
inner joinmovie_sessions
onmovie_sessions
.id
=movie_theater_rooms
.movie_session_id
wheremovie_sessions
.movie_theater_id
= 1)'
我哪里错了?
最佳答案
异常
Object of class Illuminate/Database/Eloquent/Builder could not be converted to string in .../vendor/illuminate/support/Str.php on line 338
被抛出是因为您没有在范围定义中提供 $query 参数。您的代码将尝试将电影院 ID 转换为 Builder 实例,但失败了。
尝试将您的代码重构为:
/**
* Scope a query to only select movies that are showing in a certain theater.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param int $movieTheaterId
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeGetMoviesShowingTodayOnMovieTheater($query, $movieTheaterId)
{
return $query
->whereHas('sessions.movieTheaterRoom', function ($query) use
($movieTheaterId) {
$query->whereMovieTheaterId($movieTheaterId);
});
}
我还在 whereHas
语句中添加了点符号来查询嵌套关系。
关于php - 如何获取正在电影院播放的所有电影?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55588086/