Home > Enterprise >  Laravel DI value is empty
Laravel DI value is empty

Time:11-22

This one works

     public function show($id)
    {
        $request = ClientRequest::find($id);
        return view('show', compact('request'));
    }

This one does not work!! dd($request) indicates null atrributes

    public function show(ClientRequest $request)
    {
        return view('show', compact('request'));
    }

This is my route

Auth::routes();

Route::resource('pioneer', RequestController::class);

Model

class ClientRequest extends Model
{
    use HasFactory;
    protected $guarded = [];
}

Anyone knows where the problem is ? I've googled, but still cannot figure out.

CodePudding user response:

As mentioned in my comment, with route model binding the URL segment and function parameter name must match.

Your URL segment is pioneer as defined by your Resource route definition, therefore you need to use pioneer as your function parameter name.

public function show(ClientRequest $pioneer)
{
    return view('show', compact('pioneer'));
}
  • Related