亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Laravel 控制器中訪問 JSON 數組

在 Laravel 控制器中訪問 JSON 數組

PHP
一只名叫tom的貓 2023-07-30 13:33:26
我目前對 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'];

        }

    }

  }


查看完整回答
反對 回復 2023-07-30
?
慕無忌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 

      }

  }



查看完整回答
反對 回復 2023-07-30
  • 2 回答
  • 0 關注
  • 160 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號