Argument 1 passed must be an instance of App\Request, instance of Illuminate\Http\Request given

点点圈 提交于 2019-12-07 14:24:58

问题


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:

/**
* 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" :

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?

Thanks in advance.


回答1:


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.



来源:https://stackoverflow.com/questions/45191753/argument-1-passed-must-be-an-instance-of-app-request-instance-of-illuminate-htt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!