简体   繁体   English

Laravel检索多行数据

[英]Laravel retrieving multiple rows of data

I have a 3-way relationship query. 我有一个三向关系查询。 I want to get a session for a movie which also has a cinema. 我想为一部还有电影的电影开个会。

I have access to the movie id as $id passed into the parameter. 我可以访问电影ID,因为$ id传递给参数。 I want to find the cinemas which have that movie id. 我想找到有电影身份的电影院。

Here is my controller: 这是我的控制器:

public function ticketpage($id)
{
    $movie = Movie::find($id);
    $cinemas = Cinema::all();
    $sessions = Session::all();
    $query = Session::where('movie_id', "$id")->get();
    $count = count($query)-1;


    $cinemaId = $query[$count]['cinema_id'];
    $cinemaRow = Cinema::where('id', "$cinemaId")->get();

    return view('movies/ticketpage/index')
        ->with('cinemas', $cinemas)
        ->with('sessions', $sessions)
        ->with('movie', $movie)
        ->with('cinemaRow', $cinemaRow);
}

As you can see I'm not really sure how I can use my $query (retrieving all sessions with that movie id) to retrieve all the rows of that session that have that cinema id. 正如您所看到的,我不确定如何使用我的$ query(检索具有该影片ID的所有会话)来检索具有该影院ID的该会话的所有行。

I've used $query[count]['cinema_id] but it will only return one value, the value of the last row. 我使用了$ query [count] ['cinema_id],但它只会返回一个值,即最后一行的值。 I want it to return the values of all the rows. 我希望它返回所有行的值。 I tried using a for loop but didn't have much success. 我尝试使用for循环,但没有取得多大成功。

Thanks for your help! 谢谢你的帮助!

First of all, I would assume the following schema and relationship is being set up. 首先,我假设正在设置以下架构和关系。

| Movie | Cinema | Session   |
| ----- | ------ | --------- |
| id    | id     | id        |
|       |        | cinema_id |
|       |        | movie_id  |

This is how I will fetch the cinemas that is playing the movie by its movie_id based on your statement: 这就是我将根据你的声明通过movie_id获取正在播放电影的电影院的方式:

I have access to the movie id as $id passed into the parameter. 我可以访问电影ID,因为$ id传递给参数。 I want to find the cinemas which have that movie id. 我想找到有电影身份的电影院。 .

public function ticketpage($id)
{
    // Get all Cinema which has the Session with the Movie.id
    $cinemas = Cinema::whereHas('sessions', function ($query) use ($id) {
        $query->where('movie_id', $id);
    })->get();

    return view('movies/ticketpage/index', [
        'cinemas' => $cinemas
    ]);
}

try this : 试试这个 :

public function ticketpage($id)
{
$movie = Movie::find($id);
$cinemas = Cinema::all();
$sessions = Session::all();
$query = Session::select('cinema_id')->where('movie_id', $id)->get();
//$count = count($query)-1;


//$cinemaId = $query[$count]['cinema_id'];
//$cinemaRow = Cinema::where('id', $cinemaId)->get();
$cinemaRow = Cinema::whereIn('id', $query)->get();

return view('movies/ticketpage/index', compact('cinemas', 'sessions', 'movie', 'cinemaRow'));

// No need for all the 'withs'
//return view('movies/ticketpage/index')
//    ->with('cinemas', $cinemas)
//    ->with('sessions', $sessions)
//    ->with('movie', $movie)
//    ->with('cinemaRow', $cinemaRow);
}

Replace these two in your code and try it should work : 在代码中替换这两个并尝试它应该工作:

$query = Session::select('cinema_id')->whereMovie_id($id)->get();
$cinemaWithSession = Cinema::whereId($query)->get();

If you have the movie id and there is a movie_id column in cinemas table you can retrieve cinemas using this query: 如果你有movie id并且cinemas表中有一个movie_id列,你可以使用这个查询检索电影:

public function ticketpage($id)
{
    $cinemas = Cinema::where('movie_id',$id)->get();
    return view('movies.ticketpage.index',compact('cinemas'));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM