传入的参数 1 必须是 AppRequest 的实例,给定的 IlluminateHttpRequest 的实例 [英] Argument 1 passed must be an instance of AppRequest, instance of IlluminateHttpRequest given

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

问题描述

我在我的用户模型中创建了一个方法来为用户上传海报(有干预):

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 AppUser::storePoster() must be an instance of AppRequest, instance of IlluminateHttpRequest given, called in C:xampphtdocslaravellaravel-paper-dashboardappHttpControllersUserController.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 IlluminateHttpRequest 在类的顶部告诉它使用哪个 Request 类.

You need to use the same request class in the controller and the model, so in you user model add use IlluminateHttpRequest at the top of the class to tell it which Request class to use.

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

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