-1

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.

tereško
  • 58,060
  • 25
  • 98
  • 150
  • By default, Laravel uses the last segment of the url as the placeholder of the request or a resource route, so in your controller method you can inject your model but with a different name `public function show(Usuario2 $prurequests)` – Chinonso Chukwuogor Aug 21 '18 at 18:13
  • the question is unreadable due to typos, spelling and grammar mistakes. i cannot understand what is being asked here. downvoted. – szaman Sep 05 '19 at 14:01

2 Answers2

1

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

When you try to implicit binding in resource controller then at time of creating resource controller you have to add an --model option

php artisan make:controller PrurequestsController --resource --model=Usuario2

Specifying The Resource Model If you are using route model binding and would like the resource controller's methods to type-hint a model instance, you may use the --model option when generating the controller:

Check details https://laravel.com/docs/5.6/controllers#resource-controllers

rkj
  • 8,067
  • 2
  • 27
  • 33
  • 1
    You don't add placeholders when using `Route::resource()`. – linuxartisan Aug 21 '18 at 05:15
  • i asked because i saw this video https://www.youtube.com/watch?v=fAtQ5w9QDoU&index=20&list=PLIddmSRJEJ0sxS-RmqdRMlkyWOQWvEGEt min 10:00 and here he use implicit binding in a resource without a placeholder. – Diego Izquierdo Dussan Aug 21 '18 at 15:55
  • i asked because i saw this video https://www.youtube.com/watch?v=fAtQ5w9QDoU&index=20&list=PLIddmSRJEJ0sxS-RmqdRMlkyWOQWvEGEt min 10:00 and here he use implicit binding in a resource without a placeholder. he pass ans route like ´ – Diego Izquierdo Dussan Aug 21 '18 at 16:03
  • @DiegoIzquierdoDussan it is working for me with resource too. updated answer check it – rkj Aug 21 '18 at 16:29