Laravel 仅考虑请求中所需的列,并忽略任何其他键值(如果存在)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48582673/
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
Laravel consider only the required columns from the request and ignore any other key values if present
提问by Vrajesh Doshi
In the Laravel API, I am passing the request input json with few additional key:values which I require in other part of the business logic of the API function. When I pass the array $request->all()
, of the formal parameter Request $request
of the Controller function to the Model function and directly pass it to the Eloquent create()
function as follows:
在 Laravel API 中,我传递了请求输入 json 和一些额外的 key:values,我需要在 API 函数的业务逻辑的其他部分。当我将Controller函数的形$request->all()
参数组,传递Request $request
给Model函数,直接传递给Eloquentcreate()
函数,如下:
StatusModel::create($request);
I get the error,
我得到错误,
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'app' in 'field list' (SQL: update
tbl_points
setteam_id
= 4,tour_id
= 10,match_id
= 254,win
= 0,loss
= 1,tie
= 1,n_r
= 1,pt
= 1,nrr
= 1,app
= 3 where (team_id
= 4 andtour_id
= 10 andmatch_id
= 254)).
SQLSTATE[42S22]:未找到列:1054 '字段列表'中的未知列'app'(SQL:更新
tbl_points
集team_id
= 4,tour_id
= 10,match_id
= 254,win
= 0,loss
= 1,tie
= 1,n_r
= 1,pt
= 1,nrr
= 1,app
= 3 其中 (team_id
= 4 andtour_id
= 10 andmatch_id
= 254)).
I want to pass the input request array as it is and want the laravel to ignore the columns name keys from the array which are not present in the database. EG: Following is my input json, in which "app":3 is an extra key value not present in table.
我想按原样传递输入请求数组,并希望 Laravel 忽略数据库中不存在的数组中的列名称键。EG:以下是我的输入 json,其中 "app":3 是表中不存在的额外键值。
{
"team_id": 4,
"tour_id": 10,
"match_id": 254,
"win": 0,
"loss": 1,
"tie": 1,
"n_r": 1,
"pt": 1,
"nrr": 1,
"app": 3
}
My Model Code
我的型号代码
<?php
namespace App\Models\BaseModels;
use Illuminate\Database\Eloquent\Model;
class TablePoints extends Model
{
protected $table = 'tbl_points';
protected $fillable = ['team_id','tour_id','match_id','win','loss','tie','n_r','pt','nrr'];
public $timestamps = false;
}
On dd($request->all()) I get the following in output:
在 dd($request->all()) 我得到以下输出:
array:10 [
"team_id" => 4
"tour_id" => 10
"match_id" => 254
"win" => 0
"loss" => 1
"tie" => 1
"n_r" => 1
"pt" => 1
"nrr" => 1
"app" => 3
]
How to avoid getting such errors by making code ignore extra key value pairs.
如何通过让代码忽略额外的键值对来避免出现此类错误。
Note: I don't want to create a new array and copy values of required keys from request array and use it. Is there any other solution?
注意:我不想创建一个新数组并从请求数组中复制所需键的值并使用它。还有其他解决方案吗?
回答by Laerte
You should use except
function. Try this:
你应该使用except
函数。尝试这个:
StatusModel::create($request->except('app'));
This will return all fields except for app
field.
这将返回除字段之外的所有字段app
。
You can also use it with an array to ignore multiple fields. Ex:
您还可以将它与数组一起使用以忽略多个字段。前任:
$request->except(['field1', 'field2']);
If you need to exclude all irrelevant data, you can use a code hack like this:
如果您需要排除所有不相关的数据,您可以使用这样的代码破解:
In StatusModel:
在状态模型中:
public function getFillable()
{
return $this->fillable;
}
And then in Controller, use only
method to filter the attributes in request:
然后在Controller中,使用only
方法过滤请求中的属性:
$statusModel = new StatusModel();
$fields = $request->only($statusModel->getFillable());
$statusModel->fill($fields);
$statusModel->save();