php Laravel 5 Illuminate\Http\Request 具有不允许静态调用的方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28413985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:51:15  来源:igfitidea点击:

Laravel 5 Illuminate\Http\Request has method not allowing static call

phplaravel

提问by Life4Cali

I'm trying to call the Illuminate\Http\Request has method from one of my controllers.

我正在尝试从我的一个控制器调用 Illuminate\Http\Request has 方法。

Request::has('fields')

Following from the documentation exactly, yet I'm getting an error thrown:

完全遵循文档,但我收到了一个错误:

Non-static method Illuminate\Http\Request::has() should not be called statically, assuming $this from incompatible context

非静态方法 Illuminate\Http\Request::has() 不应静态调用,假设 $this 来自不兼容的上下文

I'm not sure what I'm doing wrong here, I tried following the documentation as closely as possible.

我不确定我在这里做错了什么,我尝试尽可能密切地遵循文档。

回答by lukasgeiter

The problem is you are using the wrong Requestclass. You need to import the Facade:

问题是您使用了错误的Request类。您需要导入 Facade:

use Illuminate\Support\Facades\Request;

回答by Tanvir Gaus

The use Illuminate\Support\Facades\Request; is using the Facade which is not good practice. should use Illuminate\Http\Request; and get the request available via the method or constructor ie.-

使用 Illuminate\Support\Facades\Request; 正在使用 Facade,这不是很好的做法。应该使用 Illuminate\Http\Request; 并通过方法或构造函数获取请求,即。-

public function __construct(Request $request) {
    $this->request = $request;
}

and then in the method use it, ie.

然后在方法中使用它,即。

public function checkText() {
   $txt = $this->request->has('txt'); 
   return $txt;
}

回答by Joey

Try using Input::has() instead then use Input::get() to actually grab the corresponding POSTED/GET var.

尝试使用 Input::has() 而不是使用 Input::get() 来实际获取相应的 POSTED/GET var。