Search code examples
laravellaravel-5.6laravel-bladelaravel-resource

The implicit binding in laravel (5.6) returt a empty object


i'm trying to use the method 'show' in a controller but when it return a empty object.

Since this view:

@foreach ($usuarios as $usuario2)
  <h2>{{$usuario2->nombre}}</h2>
  <a href="prurequests/{{$usuario2->id}}">ver mas2...</a>
@endforeach

Through tis route:

Route::resource('/prurequests','PruebasControllers\PrurequestsController'); 

To this controller's method:

public function show(Usuario2 $usuario2)  // Ruta con implicing biding
 {
     return $usuario2;
 }

This is the model:

class Usuario2 extends Model
{
    Protected $fillable = ['nombre'];
}

I tried with this and it works

View:

@foreach ($usuarios as $usuario2)
  <h2>{{$usuario2->nombre}}</h2>
  <a href="impli/{{$usuario2->id}}">ver mas...</a>
  <a href="prurequests/{{$usuario2->id}}">ver mas2...</a>
@endforeach

Route

Route::get('impli/{usuario2}', function 
(fractalwebpage\PruebasModelos\Usuario2 $usuario2) {
  return $usuario2;
});

It bring me tha data ubt i had to put the nae of the model in the route and. e need to do it but Through the controller.


Solution

  • By default, Laravel uses the last segment of the url as the placeholder of the request of a resource route, so in your controller method you can inject your model but with a different name public function show(Usuario2 $prurequests)

    But a more definite solution would be to just change the parameter in the route definition

    Route::resource('/prurequests','PruebasControllers\PrurequestsController', ['parameters' => ['prurequests' => 'usuario2']]); 
    

    This way, you can continue using your controllers the way they currently are

    public function show(Usuario2 $usuario2)  // Ruta con implicing biding
    {
       return $usuario2;
    }