我在我的用户模型中创建了一个方法,为用户上传海报(带干预):
/**
* 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();
}
}
在我的UserController中,我有另一个方法来调用在User模型中创建的静态方法:
/**
* Store user's poster.
*/
public function poster(Request $request)
{
User::storePoster($request);
}
这是我的路线:
Route::post('/user-profile/store/poster', 'UserController@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
虽然如果我从模型中移动所有逻辑并将其放入我的UserController中,它可以正常工作。 知道为什么吗?
提前致谢。
答案 0 :(得分:24)
您需要在控制器和模型中使用相同的请求类,因此在您的用户模型中,在类的顶部添加use Illuminate\Http\Request
以告诉它使用哪个Request类。