Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 552
Marco Avatar asked Jul 19 '17 13:07

Marco


1 Answers

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.

like image 148
Khalid Dabjan Avatar answered Nov 14 '22 03:11

Khalid Dabjan