Php 通过的Laravel5.8模型绑定路由Arg 1必须是模型的实例

Php 通过的Laravel5.8模型绑定路由Arg 1必须是模型的实例,php,laravel,laravel-routing,laravel-5.8,Php,Laravel,Laravel Routing,Laravel 5.8,Laravel项目实施本地化,工作良好。由于用户可以在DB中保存语言(语言环境),所以进行自定义,问题就来了 用户能够在数据库中保存语言 当用户试图编辑语言(区域设置)时,就会遇到这个问题 传递给App\Http\Controllers\CustomizeController::edit()的参数1必须是App\Model\Customize的实例,字符串为 在CustomizeController.php中 public function index() { $data = a

Laravel项目实施本地化,工作良好。由于用户可以在DB中保存语言(语言环境),所以进行自定义,问题就来了

  • 用户能够在数据库中保存语言
  • 当用户试图编辑语言(区域设置)时,就会遇到这个问题
  • 传递给App\Http\Controllers\CustomizeController::edit()的参数1必须是App\Model\Customize的实例,字符串为

    在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);
    }
    
    路由:列出命令O/p,如下所示:

    GET|HEAD  | {locale}/customize/{customize}/edit | customize.edit | App\Http\Controllers\CustomizeController@edit | web,setlocale,auth
    
    甚至尝试从blade硬编码为:

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

    完成项目

    错误很简单

    调用
    edit
    方法时,路径参数后跟定义从容器中获取的对象

    由于将
    customize
    参数传递给路由,因此方法参数的定义应如下所示:

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

    现在我必须修改我的控制器,比如
    公共函数编辑($locale,Customize$Customize)
    公共函数更新($locale,Request$Request,Customize$Customize)
    。是否有可能在某个地方进行全局更改,而不是在每个控制器中进行更改???????????????不管怎样,它的工作正如预期!!谢谢