传递的参数1必须是App \ Request的实例,Illuminate \ Http \ Request的实例已给定 [英] Argument 1 passed must be an instance of App\Request, instance of Illuminate\Http\Request given

查看:281
本文介绍了传递的参数1必须是App \ Request的实例,Illuminate \ Http \ Request的实例已给定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在用户模型中创建了一种方法,用于为用户上传海报(带有干预):

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();

    }

}

在我的UserController中,我还有另一个方法可以调用在User模型中创建的静态方法:

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);

}

这是我的路线:

Route::post('/user-profile/store/poster', 'UserController@poster');

这是我导航到"/user-profile/store/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

尽管如果我从模型中移走所有逻辑并将其放在我的UserController中,它也可以正常工作. 知道为什么吗?

Although if I move all the logic from the model and put it in my UserController it works fine. Any idea why?

提前谢谢.

推荐答案

您需要在控制器和模型中使用相同的请求类,因此在用户模型中,在类顶部添加use Illuminate\Http\Request可以告诉它使用哪个Request类.

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.

这篇关于传递的参数1必须是App \ Request的实例,Illuminate \ Http \ Request的实例已给定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆