问题描述
我想获取不同的表数据并将其保存在数组中。
I want to get different table data and save them in array.
$int_payment = IntPayments::where('month',$month_year)->groupBy('interviewer_id')->get();
foreach ($int_payment as $inter) {
$inters[] = User::where('id',$inter->interviewer_id)->get();
$inters['payment_status'] = $inter->payment_status;
}
return response()->json(['inters'=>$inters,'month'=>$month]);
在这里,我要从int_payment表的visiter_id中获取用户表中的用户名。现在 $ inters []
拥有特定用户的所有数据。现在,我只想从int_payments表中获取一列,并将其添加到$ inters数组中。上面的代码无法正常工作。结果。上面的代码还将付款状态添加到第一个数组值,而不是其他数组值。像这样
Here i am fetching the name of the user in user's table from interviewer_id which is from int_payment table. Now the $inters[]
has all the data of the particular user. Now i want to fetch only one column from the int_payments table and add it to the array $inters. The above code doesn't work as expected. it results this. The above code also adds the the payment status to the first array value and not to the other array values. like this
{"inters":{"0":[{"id":1,"name":"Naveen1","phone":"7841237900",}],"payment_status":"unpaid","1":[{"id":2,"name":"john","phone":"7845123900",}]}}
{"inters":{"0":[{"id":1,"name":"Naveen1"}],"payment_status":"unpaid",}
但是我希望它包含在这样的数组中
But i want it to be include inside the array like this
{"inters":{"0":[{"id":1,"name":"Naveen1","payment_status":"unpaid"}],}
{"inters":{"1":[{"id":2,"name":"john","payment_status":"paid"}],}
推荐答案
如果我正确理解了您想要的东西,
If I got you correctly that you want to have something like this:
{"inters":{"0":[{"id":1,"name":"Naveen1","payment_status":"unpaid"}],}
也许使用 push
在集合上对您有帮助:
The perhaps using push
on the collection you have would be helpful as:
$int_payment = IntPayments::where('month',$month_year)->groupBy('interviewer_id')->get();
foreach ($int_payment as $key => $inter) {
$inters = User::where('id',$inter->interviewer_id)->get(['id', 'name']);
$inters_payment[] = $inters->push(['payment_status' => $inter->payment_status]);
}
return response()->json(['inters'=>$inters_payment,'month'=>$month]);
顺便说一句,我不知道这个 $每月在哪里
变量将起作用,但我只是在示例代码中保留了它。
By the way, I don't know where this $month
variable will feature, but I just left it as you have in your example code.
但是,一个警告是,为什么不采用laravel的关系来满足用户
和 IntPayments
模型之间的交互?因为正如我所观察到的,如果您返回了数千个 IntPayments
,那么仅就此功能而言,您将在个用户上执行数千个查询
表。
One warning however, is that why not adopt laravel's relationship to cater for the interaction between the User
and IntPayments
model? Because as I could observe, if you have thousands of IntPayments
returned, then just for this function, you would be performing thousands of query on users
table.
您可能会遇到一些简单而优雅的事情:
You could have something simple and elegant as this:
$inters = IntPayments::where('month',$month_year)->groupBy('interviewer_id')->with('user')->get();
$result = [];
foreach($inters as $key => $inter)
{
$result[$key]['id'] = $inter->user->id;
$result[$key]['name'] = $inter->user->name;
$result[$key]['payment_status'] = $inter->payment_status;
}
然后,您的响应如下:
return response()->json(['inters'=> $result]);
这篇关于在laravel中将不同的表数据合并到相同的数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!