laravel 传递的参数 1 必须是 App\Request 的实例,给定的 Illuminate\Http\Request 的实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45191753/
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
Argument 1 passed must be an instance of App\Request, instance of Illuminate\Http\Request given
提问by Marco
I have created a method in my User model to upload a poster (with intervention)for the user:
我在我的用户模型中创建了一个方法来为用户上传海报(有干预):
/**
* Store user's poster.
*/
public static function storePoster(Request $request)
{
if($request->hasFile('posterUpload')){
$poster = $request->file('posterUpload');
$filename = time() . '.'. $poster->getClientOriginalExtension();
Image::make($poster)->resize(356,265)->save(public_path('/uploads/posters/'.$filename));
$check = Setting_user::where([
['user_id', '=' ,Auth::user()->id],
['setting_id','=', 2],
])->first();
if(!$check)
{
$setting = new Setting_user();
$setting->user_id = Auth::user()->id;
$setting->setting_id = 2;
$setting->value = $filename;
$setting->save();
return back();
}
$check->value = $filename;
$check->update();
return back();
}
}
In my UserController I have another method which call the static method created in the User model:
在我的 UserController 中,我有另一个方法调用在 User 模型中创建的静态方法:
/**
* Store user's poster.
*/
public function poster(Request $request)
{
User::storePoster($request);
}
This is my route:
这是我的路线:
Route::post('/user-profile/store/poster', 'UserController@poster');
And this is the error I get when I navigate to "/user-profile/store/poster" :
这是我导航到 "/user-profile/store/poster" 时遇到的错误:
Argument 1 passed to App\User::storePoster() must be an instance of App\Request, instance of Illuminate\Http\Request given, called in C:\xampp\htdocs\laravel\laravel-paper-dashboard\app\Http\Controllers\UserController.php on line 29 and defined
Although if I move all the logic from the model and put it in my UserController it works fine. Any idea why?
虽然如果我从模型中移动所有逻辑并将其放入我的 UserController 中,它工作正常。知道为什么吗?
Thanks in advance.
提前致谢。
回答by Khalid Dabjan
You need to use the same request class in the controller and the model, so in you user model add use Illuminate\Http\Request
at the top of the class to tell it which Request class to use.
您需要在控制器和模型中使用相同的请求类,因此在您的用户模型中use Illuminate\Http\Request
,在类的顶部添加以告诉它使用哪个请求类。