我目前對 Vue 組件發送到 Laravel 控制器的一些數據有點困惑。數據如下:array:2 [ 0 => {#1246 +"id": 1 +"name": "Developer" } 1 => {#1249 +"id": 2 +"name": "Ops Matrix Admin" }]例如,如果我想從中獲取 thename或 theid作為對象,以便我可以通過 eloquent 來使用它。我該怎么做呢?這就是我的控制器目前的樣子。 public function editUserPermissions(Request $request, $id) { foreach($request->all() as $key => $value) { $decode = json_decode($value); dd($decode); } }當我這樣做時,dd($request->all());我得到以下信息:array:1 [ "body" => "[{"id":1,"name":"Developer"},{"id":3,"name":"Ops Matrix User"}]"]
2 回答

慕姐8265434
TA貢獻1813條經驗 獲得超2個贊
您需要循環遍歷結果。結果是一個數組。
更好的方法是使用 $request->getContent()
但是使用你的代碼
public function editUserPermissions(Request $request, $id) {
foreach($request->all() as $key => $value) {
$decode = json_decode($value);
foreach($decode as $decoded) {
echo $decoded['id'];
}
}
}

慕無忌1623718
TA貢獻1744條經驗 獲得超4個贊
public function editUserPermissions(Request $request, $id) {
$bodys = $request->body;
foreach($bodys as $key => $body) {
//$key give current index of array
$body[$key]['id'] //this give id
$body[$key]['name'] //this give name
}
}
- 2 回答
- 0 關注
- 160 瀏覽
添加回答
舉報
0/150
提交
取消