我有以下雄辩的实体:
表结构:
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 < / strong>属于电影。
我正在尝试的是,通过$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可恢复的致命错误:不能在行338上的... / vendor / illuminate / support / Str.php中将类Illuminate / Database / Eloquent / Builder的对象转换为字符串
我也曾尝试像这样使用此软件包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()
这是输出:
带有消息“ SQLSTATE [42S22]”的Illuminate / Database / QueryException:找不到列:1054“字段列表”中的未知列“ movie_sessions.movie_theater_id”(SQL:选择
movies
。*,movie_sessions
。movie_theater_id
来自{{1}上的movies
内部联接movie_theater_rooms
。movie_theater_rooms
=id
。movies
内部联接movie_theater_room_id
在movie_sessions
上。movie_sessions
=id
。movie_theater_rooms
其中movie_session_id
。movie_sessions
= 1)'
我错了吗?
答案 0 :(得分:1)
例外
之所以抛出Illuminate / Database / Eloquent / Builder类的对象无法在第338行的... / vendor / illuminate / support / Str.php中转换为字符串
,是因为您没有在范围定义中提供$ 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
语句中添加了点符号来查询嵌套关系。
答案 1 :(得分:0)