[英]Laravel create an array in postman and save in database
I am having troubles at this situation. 我在这种情况下遇到了麻烦。 This is what I show in Postman 这就是我在邮递员中展示的
Controller 调节器
public function index(Request $request)
{
$user = Auth::guard('api');
if ($user)
{
$parameters=new UserParameters();
$parameters->id_user=Auth::guard('api')->id();
$parameters->day=$request->day;
if($parameters->save())
{
$times=$request->time;
foreach ( $times as $time)
{
$new_time = new TimeParameter();
$new_time->id_parameter = $parameters->id;
$new_time->time = $time[0];
$new_time->hour = $time[1];
$new_time->save();
}
return response()->json(['true' => false, 'message' => 'Saved!']);
}
}
else
{
return response()->json(['error' => false, 'message' => 'User not found!']);
}
}
I stuck at for loop. 我陷入了循环。 How can I handle this situation ? 我该如何处理这种情况?
Problem 问题
I don't know how your code works, because this for ( $times as $time)
is not how for
works. 我不知道你的代码是如何工作的,因为这for ( $times as $time)
并不怎么样for
作品。
Here is a explanation on for
https://secure.php.net/manual/pt_BR/control-structures.for.php 这是for
https://secure.php.net/manual/pt_BR/control-structures.for.php的解释
Solution 解
The as
on the for
make me think your trying to use the foreach
. for
上的as
使我认为您尝试使用foreach
。 And here is how it works: 这是它的工作方式:
foreach( $times as $time)
the $time
is already a single record from the array
foreach( $times as $time)
$time
已经是array
一条记录
So when you do a foreach
this is how it should look: 因此,当您执行foreach
时,它应该是这样:
foreach ( $times as $time){
$new_time = new TimeParameter();
$new_time->id_parameter = $parameters->id;
$new_time->time = $time->time;
$new_time->hour = $time->hour;
$new_time->save();
}
This is because of how your key's are named. 这是因为密钥的命名方式。 time[0].time is not valid and maybe it is reading it to time[0] only. time [0] .time无效,并且可能仅将其读取为time [0]。 Which is why you are only getting the hours. 这就是为什么您只得到时间。
time[0].time = 'morning'; time [0] .time ='早上';
time[0].hour = '10:00'; time [0] .hour = '10:00';
- then it will not count the .time or the .hour because that is not valid. -然后它将不计算.time或.hour,因为那是无效的。 then it would only make it time[0] which then explains why you are only getting the hours. 那么只会使它成为时间[0],这便说明了为什么只有时间。
With the changes on the postman data that I asked you to do. 随着我要求您对邮递员数据进行的更改。 You can try this solution. 您可以尝试此解决方案。
public function index(Request $request)
{
$user = Auth::guard('api');
if ($user)
{
$parameters=new UserParameters();
$parameters->id_user=Auth::guard('api')->id();
$parameters->day=$request->day;
if($parameters->save())
{
$data= $request->all();
foreach ( $data['times'] as $time)
{
$new_time = new TimeParameter();
$new_time->id_parameter = $parameters->id;
$new_time->time = $time[0];
$new_time->hour = $time[1];
$new_time->save();
}
return response()->json(['true' => false, 'message' => 'Saved!']);
}
}
else
{
return response()->json(['error' => false, 'message' => 'User not found!']);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.