Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Enerus's avatar

Getting Id from object when sent to next page

Hello i'm still pretty new to this and learning, sorry if the answer is obvious. I'm doing a small project where I would show a list of movies and when you click on one it takes you to that movie's page where you can see more details. Here is what I have so far: Controller

public function getShow($id)
{
	$arrayPeliculas= \DB::table('movies');
    return view('catalog.show', ['arrayPeliculas' => Movie::all()]);
}

public function getEdit($id)
{
    return view('catalog.edit', array('id'=>$id));
}

public function getIndex()
{
	$arrayPeliculas= \DB::table('movies');
	return view('catalog.index', ['arrayPeliculas' => Movie::all()]);
}

public function getCreate()
{
    return view('catalog.create');
}
Index file where list of movies is show:
    @foreach( $arrayPeliculas as $key => $pelicula )
    <div class="col-xs-6 col-sm-4 col-md-3 text-center">

        <a href="{{ url('/catalog/show/' . $key ) }}">
            <img src="{{$pelicula->poster}}" style="height:200px"/>
            <h4 style="min-height:45px;margin:5px 0 10px 0">
                {{$pelicula['title']}}
            </h4>
        </a>

    </div>
    @endforeach

Details page

	    <div class="col-sm-4">
        {{-- TODO: Imagen de la película --}}
        <img src="{{$arrayPeliculas[1]->poster}}" style="height:200px"/>
         </div>

    <div class="col-sm-8">

        {{-- TODO: Datos de la película --}}
        <h1>{{ $arrayPeliculas[1]->title }}</h1>
        <h3>Año: {{ $arrayPeliculas[1]->year }}</h3>
        <h3>Director: {{ $arrayPeliculas[1]->director }}</h3>
        <p><b>Resumen:</b>{{  $arrayPeliculas[1]->synopsis }}</p>
        
        @if($arrayPeliculas[1]->rented==true)
            <div>
                <p><b>Estado:</b>Pelicula actualmente alquilado</p>
                <button class="btn btn-danger" type="submit">Devolver Pelicula</button>
                <button class="btn btn-warning" type="submit">Editar Pelicula</button>
                <button class="btn btn-default" type="submit">Volver al listado</button>
            </div>
        @else
            <div>
                <p><b>Estado:</b>Pelicula Disponible</p>
                <button class="btn btn-primary" type="submit">Alquilar Pelicula</button>
                <a href="{{ url('/catalog/edit/' . $arrayPeliculas[0]->id ) }}"><button class="btn btn-warning" type="submit">Editar Pelicula</button></a>
                <a href="/videoclub/public/catalog"><button class="btn btn-default" type="submit">Volver al listado</button></a>
            </div>
        @endif
        
    </div>

So with index I was able to use a foreach that list all the movies and then send it to the correct page of that movie. On the details page though i'm not quite able to figure out how to get the id of that movie to show the relevant info. I tried using $arrayPeliculas->poster Then I tried putting the index which sort of works, but only shows the movie of that index no matter the page im in $arrayPeliculas[1]->poster I imagine it should be something like $arrayPeliculas[$id]->poster but i'm not able to figure out how to get the id for the selected movie.

0 likes
6 replies
Snapey's avatar

First off

	$arrayPeliculas= \DB::table('movies');  // this line is doing absolutely nada

	return view('catalog.index', ['arrayPeliculas' => Movie::all()]);

Please show your routes. You need to pass the ID of the movie via the request.

I suggest you follow the 'from scratch' series

1 like
Enerus's avatar

Oh wow you're right, I didn't realize. Here are the routes

Route::get('catalog', 'App\Http\Controllers\CatalogController@getIndex');

Route::get('catalog/show/{id}', 'App\Http\Controllers\CatalogController@getShow');

Route::get('catalog/create', 'App\Http\Controllers\CatalogController@getCreate');

Route::get('catalog/edit/{id}', 'App\Http\Controllers\CatalogController@getEdit');

Enerus's avatar

Ok I think I got it, I added $id = request()->id Then I used this {{$arrayPeliculas[$id]->poster}} I'll definitely watch that from scratch series, I just needed some help since this is for a project due in a few hours and we were only given a rough idea of how to do everything.

Snapey's avatar
Snapey
Best Answer
Level 122
public function getShow($id)
{
    return view('catalog.show', ['movie' => Movie::findOrFail($id)]);
}

Then in the view, you can access all properties of $movie with arrow, eg {{ $movie->title }}

1 like
Enerus's avatar

I see, thanks, I like it better that way.

jpmg's avatar

Hello... in your view show you are doing

  @foreach( $arrayPeliculas as $key => $pelicula )
         ........
         <a href="{{ url('/catalog/show/' . $key ) }}">
            ........
         </a>
    @endforeach

your route is ok

Route::get('catalog/show/{id}', 'App\Http\Controllers\CatalogController@getShow');

and in your controller you can do something like this.

 public function getShow(Movie $id)  
   {
          $movies=$id;
          return view('catalog.show', ['arrayPeliculas' =>$movies]);
   }
1 like

Please or to participate in this conversation.