【问题标题】:Passing the data of database in json object in laravel在laravel的json对象中传递数据库的数据
【发布时间】:2017-08-04 10:10:18
【问题描述】:

好吧,这就是我所做的:

     public function api_index()
{

    $agents=DB::table('users')
        ->select('id','username','role')
        ->get();

    return response()->json($agents);
}

它在浏览器中抛出这个:

     [{"id":"1","username":"suzan","role":"Admin"},       {"id":"2","username":"momta","role":"admin"}]

但我实际上需要的是我需要一个 json 对象,并且在该 json 对象上我需要这些数据。可能是这样的:

    {"jsonboject":[{"id":"1","username":"suzan","role":"Admin"},{"id":"2","username":"momta","role":"admin"}]}

我该怎么做?

【问题讨论】:

    标签: json laravel


    【解决方案1】:

    试试这个

    public function api_index()
    {
    
        $agents=DB::table('users')
            ->select('id','username','role')
            ->get();
    
        return response(['jsonboject' => $agents]);
    }
    

    【讨论】:

      【解决方案2】:

      这样做:

      return response()->json(['jsonobject' => $agents]);
      

      【讨论】:

        【解决方案3】:

        执行以下代码:

             public function api_index()
        {
        
            $agents=DB::table('users')
                ->select('id','username','role')
                ->get();
        
            return response()->json(array('jsonobject'=>$agents),200);
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-04-17
          • 2020-05-30
          • 2017-01-23
          • 2020-03-14
          • 1970-01-01
          • 2017-02-17
          • 2016-11-21
          相关资源
          最近更新 更多