Ad

Laravel5.8 Model Binding Route Arg 1 Passed Must Be An Instance Of Model

Laravel Project Implemented with Localization and working fine. Making customization as the user can save the language(locale) in DB and here comes the issue.

  1. User Able to Save Language in DB.
  2. And When User try to Edit Language(locale) then getting this Issue.

Argument 1 passed to App\Http\Controllers\CustomizeController::edit() must be an instance of App\Model\Customize, string given

in CustomizeController.php

    public function index()
{
    $data = array(
        'title' =>'Customize',
        'heading' =>'List',
        'customize' => Customize::where(['user_id' => Auth::user()->id])->first(),
    );
    if ($data['customize'])
    {
        return redirect()->route('customize.edit', ['locale' => app()->getLocale(), 'customize' => $data['customize']]); // Redirect to Edit Route If Language available in DB
    }
    return view('Customize.index')->with($data);
}

route:list command O/P as below:

GET|HEAD  | {locale}/customize/{customize}/edit | customize.edit | App\Http\Controllers\[email protected] | web,setlocale,auth

Even tried to hardcode from blade as:

<a target="_blank" rel="nofollow noreferrer" href="{{ url(app()->getLocale().'/customize/1/edit') }}">
        <button type="button" class="btn btn-warning">Edit</button>
    </a>

Complete project available here

Ad

Answer

The error is pretty straight forward.

The edit method is called with the route parameters followed by the object you defined to get from the container.

Since you pass the customize param to the route, your method arguments should've been defined like follows:

public function edit(string $customize, App\Model\Customize $customizeModel)
{
    $customizeModel
        ->whereUserId(Auth::user()->id)
        ->update(['customize' => $customize]);

    $return response(); // whatever you need
}
Ad
source: stackoverflow.com
Ad