请求对象的 Laravel if 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37889280/
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-09-14 14:01:06  来源:igfitidea点击:

Laravel if statement for request object

laravelissetlumen

提问by Citti

Currently building out an API. I am working on an update function with excepts the incoming request. so: api.dev.com/?api_key=asdf&storeid=2 etc

目前正在构建一个 API。我正在处理传入请求之外的更新功能。所以:api.dev.com/?api_key=asdf&storeid=2 等

now in the backend i am accepting the Request and then i am trying to write some logic to check if the request includes storeid then update that entry etc.

现在在后端,我正在接受请求,然后我尝试编写一些逻辑来检查请求是否包含 storeid,然后更新该条目等。

But when i am writing my logic and i do

但是当我写我的逻辑时

if(isset($request->storeid))
    $store->id = $request->storeid;

If I do not include storeid in my put request then i get:

如果我的 put 请求中没有包含 storeid,那么我会得到:

Call to a member function parameter() on a non-object

Because it is not present. Am I approaching writing this logic the wrong way?

因为它不存在。我是否以错误的方式编写此逻辑?

I want to leave it up to the users to update the records they want. So they should not have to include all the variables in the request. Just those that need to be updated.

我想让用户更新他们想要的记录。所以他们不应该在请求中包含所有变量。只是那些需要更新的。

Citti

奇蒂

回答by jszobody

Do this instead:

改为这样做:

if($request->has('storeid')) {
    ...

回答by ThormaWeb.com

The error "Call to a member function parameter() on a non-object" is not because you are not including the "storeid" input, but because you are not calling an instance of the Store object.

错误“调用非对象上的成员函数参数()”不是因为您没有包含“storeid”输入,而是因为您没有调用 Store 对象的实例。

Maybe if you do something like:

也许如果你做这样的事情:

if(isset($request->storeid))
  $store = Store::findOrFail($request->storeid)