laravel 传递给 App\Http\Controllers\ProfileController::store() 的参数 2 必须是 App\Http\Requests\CreateProfileRequest 的一个实例,没有给出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30018862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:29:25  来源:igfitidea点击:

Argument 2 passed to App\Http\Controllers\ProfileController::store() must be an instance of App\Http\Requests\CreateProfileRequest, none given

phpformslaravelrequest

提问by Shebs72

I have been pulling my hair out with this error for a few hours now but cannot understand why it is doing it. I have done this before on Laravel but it just does not seem to want to work.

几个小时以来,我一直因这个错误而拉我的头发,但不明白为什么会这样。我之前在 Laravel 上做过这个,但它似乎不想工作。

This is the method I am trying to get to work:

这是我试图开始工作的方法:

public function store($name, CreateProfileRequest $request)
{
    $user = User::whereName($name)->first();


    $house = new House();
    $house->first_line_address = $request->get('first_line_address');
    $house->city = $request->get('city');
    $house->postcode = $request->get('postcode');

    $user->house()->save($house);

    return redirect('/');

}

This is the form I am trying to get the data from:

这是我试图从中获取数据的表单:

{!! Form::model($user->house, ['route' => 'profile.store']) !!}

    <div class ="form-group">
        {!! Form::label('first_line_address', 'First line of address:') !!}
        {!! Form::text('first_line_address', null, ['class' => 'form-control'])!!}
    </div>

    <div class ="form-group">
        {!! Form::label('city', 'Town/City:') !!}
        {!! Form::text('city', null, ['class' => 'form-control'])!!}
    </div>

    <div class ="form-group">
        {!! Form::label('postcode', 'Postcode:') !!}
        {!! Form::text('postcode', null, ['class' => 'form-control'])!!}
    </div>

    <div class ="form-group">
        {!! Form::submit('Update Profile', ['class' => 'btn btn-primary']) !!}
    </div>

    {!! Form::close() !!}
</div>

It is fine until I attempt to store it then I get

很好,直到我尝试存储它然后我得到

Argument 2 passed to App\Http\Controllers\ProfileController::store() must be an instance of App\Http\Requests\CreateProfileRequest, none given

传递给 App\Http\Controllers\ProfileController::store() 的参数 2 必须是 App\Http\Requests\CreateProfileRequest 的一个实例,没有给出

This is my route:

这是我的路线:

$router->resource('profile', 'ProfileController');

| GET|HEAD | profile | profile.index | App\Http\Controllers\ProfileController@index | |

| 得到|头| 简介 | profile.index | App\Http\Controllers\ProfileController@index | |

| | GET|HEAD | profile/create | profile.create | App\Http\Controllers\ProfileController@create | |

| | 得到|头| 个人资料/创建 | profile.create | App\Http\Controllers\ProfileController@create | |

| | POST | profile | profile.store | App\Http\Controllers\ProfileController@store | |

| | 发布 | 简介 | profile.store | App\Http\Controllers\ProfileController@store | |

| | GET|HEAD | profile/{profile} | profile.show | App\Http\Controllers\ProfileController@show | |

| | 得到|头| 个人资料/{个人资料} | profile.show | App\Http\Controllers\ProfileController@show | |

| | GET|HEAD | profile/{profile}/edit | profile.edit | App\Http\Controllers\ProfileController@edit | |

| | 得到|头| 个人资料/{个人资料}/编辑 | profile.edit | App\Http\Controllers\ProfileController@edit | |

| | PUT | profile/{profile} | profile.update | App\Http\Controllers\ProfileController@update | |

| | 放 | 个人资料/{个人资料} | profile.update | App\Http\Controllers\ProfileController@update | |

| | PATCH | profile/{profile} | | App\Http\Controllers\ProfileController@update | |

| | 补丁 | 个人资料/{个人资料} | | App\Http\Controllers\ProfileController@update | |

| | DELETE | profile/{profile} | profile.destroy | App\Http\Controllers\ProfileController@destroy | |

| | 删除 | 个人资料/{个人资料} | profile.destroy | App\Http\Controllers\ProfileController@destroy | |

If anyone thinks of how to get rid of this ridiculous error I will be forever grateful :)

如果有人想到如何摆脱这个荒谬的错误,我将永远感激:)

回答by N.B.

If you want an instance of Requestto be available to your controller method, it must be the first argument, followed by other arguments passed via URI. Reverse the methods.

如果您希望Request控制器方法可以使用的实例,则它必须是第一个参数,然后是通过 URI 传递的其他参数。颠倒方法。

回答by lukasgeiter

The store()method of a resource controller/route doesn't take any route parameters. That means Laravel has nothing to pass as $nameargument. I suppose you're passing the name in the request data itself. If that's the case you can access it with $request->input('name').

store()资源控制器/路由的方法不采用任何路由参数。这意味着 Laravel 没有什么可以作为$name参数传递的。我想您是在请求数据本身中传递名称。如果是这种情况,您可以使用$request->input('name').

Anyways you should remove $namefrom the method signature:

无论如何,您应该$name从方法签名中删除:

public function store(CreateProfileRequest $request)