Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从模型中的两个类返回json_Json_Laravel_Laravel 5.3 - Fatal编程技术网

从模型中的两个类返回json

从模型中的两个类返回json,json,laravel,laravel-5.3,Json,Laravel,Laravel 5.3,我试图在我的模型中用两个类返回json public function both() { return $this->belongsToMany(Car::class)->and(Driver::class)->withPivot('type'); // this is wrong } 我之所以这样做是因为在我的函数中 public function check() { $user = JWTAuth::parseToken()->authentica

我试图在我的模型中用两个类返回json

public function both()
{
    return $this->belongsToMany(Car::class)->and(Driver::class)->withPivot('type'); // this is wrong 
}
我之所以这样做是因为在我的函数中

public function check()
{
    $user = JWTAuth::parseToken()->authenticate();
    $id = $user->id;
    $result = User::with('both')->where('id', $id)->get();
    return response()->json($result);
}
现在在我的模型里我有这个

public function both()
{
    return $this->belongsToMany(Car::class)->withPivot('type');
}
public function driver()
{
    return $this->belongsToMany(Driver::class)->withPivot('type');
}
但是当
两者都结束时,该函数返回具有不同结构的json

我的问题是,我可以让函数返回具有相同结构的json吗

像这样

 [
{
"id": 4,
"name": null,
"phone": "9000",
"size_of_house": null,
"created_at": "2016-12-04 13:55:52",
"updated_at": "2017-03-08 14:03:44",
"deleted_at": null,
"both": [
  {
    "id": 177,
    "name": "NIS",
    "created_at": "2016-12-27 10:28:29",
    "updated_at": "2016-12-27 10:28:29",
    "pic": "http://localhost:8000/images/1482833485.jpg",
    "pivot": {
      "user_id": 4,
      "car_id": 177,
      "type": "car"
    }
  },

"both": [
  {
    "name": "Abdulaziz Alomhsen",
    "age": "30",
    "created_at": "2017-02-28 09:36:15",
    "updated_at": "2017-03-08 08:46:06",
    "status": "جايه",
    "pic": "http://localhost:8000/images/1488714520.jpg",
    "id": 2,
    "pivot": {
      "user_id": 4,
      "driver_id": 2,
      "type": "driver"
    }
  },

json不能包含多个键,因为您同时提供了
有更好的方法吗@Hassan什么是
和()?