Laravel 检索多行数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39989143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:35:36  来源:igfitidea点击:

Laravel retrieving multiple rows of data

phplaravelcontroller

提问by Hyman Perry

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. I want to find the cinemas which have that movie id.

我可以访问电影 id 作为 $id 传递给参数。我想找到具有该电影 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. I want it to return the values of all the rows. I tried using a for loop but didn't have much success.

我使用过 $query[count]['cinema_id] 但它只会返回一个值,即最后一行的值。我希望它返回所有行的值。我尝试使用 for 循环,但没有取得多大成功。

Thanks for your help!

谢谢你的帮助!

采纳答案by MinFu

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);
}

回答by Mosufy

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_idbased on your statement:

这就是我将movie_id根据您的陈述获取正在播放电影的电影院的方式:

I have access to the movie id as $id passed into the parameter. I want to find the cinemas which have that movie id..

我可以访问电影 id 作为 $id 传递给参数。我想找到具有该电影 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
    ]);
}

回答by smartrahat

If you have the movie idand there is a movie_idcolumn in cinemastable you can retrieve cinemas using this query:

如果您有movie id并且表中有movie_id列,cinemas您可以使用以下查询检索电影院:

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

回答by Emad Dehnavi

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();