如何获取在MovieTheater上播放的所有电影?

时间:2019-04-09 08:17:03

标签: php laravel eloquent

我有以下雄辩的实体:

  • 电影
  • MovieSession
  • MovieTheater
  • MovieTheaterRoom

表结构:

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.movi​​e_theater_id”(SQL:选择movies。*,movie_sessionsmovie_theater_id来自{{1}上的movies内部联接movie_theater_roomsmovie_theater_rooms = idmovies内部联接movie_theater_room_idmovie_sessions上。movie_sessions = idmovie_theater_rooms其中movie_session_idmovie_sessions = 1)'

我错了吗?

2 个答案:

答案 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)

如果您想使用staudenmeir/eloquent-has-many-deep,则应按以下顺序进行订购

class MovieTheater extends Model {
    use HasRelationships;

    public function movies()
    {
        return $this->hasManyDeep(Movie::class, [MovieTheaterRoom::class, MovieSession::class]);
    }
}

示例订单: enter image description here