[英]how to receive data from user with request with less code in laravel
Ι want to recive 10 variables from my front side for example so what Ι do Ι have to write like below : 我想从我的正面获取10个变量,所以我要做的事情如下所示:
$item1 = $request->get('item1');
$item2 = $request->get('item2');
$item3 = $request->get('item3');
$item4 = $request->get('item4');
$item5 = $request->get('item5');
$item6 = $request->get('item6');
Ι want to know if there is any way to write it in 1 line of code as it makes my code so messy, or any other optimized or better way to get the variables from a user with less code 我想知道是否有任何方法可以在一行代码中编写它,因为这会使我的代码变得凌乱,还是有其他优化或更好的方法可以用更少的代码从用户那里获取变量
May be you can do like below. 也许您可以像下面这样。
extract($request->only(['item1','item2','item3','item4','item5','item6']));
Laravel have except
function also. Laravel也具有except
功能。
extract($request->except(['_token','id']));// it will get array of all request data except _token and id.
More Description 更多说明
//$request->only(['item1','item2','item3']) will give you below array
$requestArr = ['item1'=>'value1','item2'=>'value2','item3'=>'value3'];
//extract will convert array key's as php variables
extract($requestArr);
// now you can use those php variable
echo $item1 ?? 'item1 is not a variable';
Inlude all the column names is in your model like below 包括所有列名都在模型中,如下所示
class User extends Model
{
protected $fillable=['item1', 'item2',...... And s on] ;
}
And in your controller: 在您的控制器中:
Public function(Request $request)
{
$user=create([$request->all()]) ;
}
And this should be work... If it is not let me know 这应该可行...如果不是让我知道
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.