Laravel 5.5。在json中包含关系表

时间:2018-07-31 14:23:45

标签: php json laravel

我尝试使用laravel 5.5和dingo在json返回中包含连接表。

我有这行:return $this->model->where("id_user", "=", $user_id)->with('typeSpeciality')->get();

我得到了这个结果,专业表不包含

{
    "surname": "Robert",
    "first_name": "Lavoie",
    "speciality_id": 1,
    "email": "eyJpdiI6ImdkUUtqeUFOc0REdmQ0WUF4VUsyTXc9PSIsInZhbHVlIjoiYWFqNXFRQ3JqUmZGRWRnU1BOTVFxam5OSHQrYUVaNE5jNGNnejhTbDdhYz0iLCJtYWMiOiJhZDhkYzc5NzVmYTE4YzNjNjE2N2JkYTFlZWM3MzNkNjU0YTE2OTcxY2JlMjc0NzZlODE4OGI2NWFiNDVkMTg5In0=",
    "practice_number": "1111",
    "gender": "M",
    "sms_phone_number": null,
    "created_at": "2012-04-25 18:13:10",
    "role_id": 6,
    "auto_accept": 0
}

专业表未包含在我的json中...我想要这样的东西

{
    "surname": "Robert",
    "first_name": "Lavoie",
    "speciality": {
                    "name": "sometext",
                    "title": "sometext",
                   }
    "email": "eyJpdiI6ImdkUUtqeUFOc0REdmQ0WUF4VUsyTXc9PSIsInZhbHVlIjoiYWFqNXFRQ3JqUmZGRWRnU1BOTVFxam5OSHQrYUVaNE5jNGNnejhTbDdhYz0iLCJtYWMiOiJhZDhkYzc5NzVmYTE4YzNjNjE2N2JkYTFlZWM3MzNkNjU0YTE2OTcxY2JlMjc0NzZlODE4OGI2NWFiNDVkMTg5In0=",
    "practice_number": "1111",
    "gender": "M",
    "sms_phone_number": null,
    "created_at": "2012-04-25 18:13:10",
    "role_id": 6,
    "auto_accept": 0
}

我的关系函数

   public function typeSpeciality()
{
    return $this->hasOne('App\Models\TypeSpeciality', "speciality_id","speciality_id");
}

做到这一点的最佳方法是什么?

3 个答案:

答案 0 :(得分:2)

将您的关系从hasOne更改为belongsTo

public function typeSpeciality()
{
    return $this->belongsTo('App\Models\TypeSpeciality', "speciality_id","speciality_id");
}

答案 1 :(得分:1)

您可以使用Eloquent: API Resources获得所需的内容:

您必须创建一个User(假设另一个模型是User)和TypeSpeciality资源:

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class TypeSpeciality extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'name' => $this->name,
            'title' => $this->title
        ];
    }
}

然后输入用户资源:

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use App\Http\Resources\TypeSpeciality as TypeSpecialityResource;

class User extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'surname' => $this->surname,
            'first_name' => $this->first_name,
            'speciality' => new TypeSpecialityResource($this->typeSpeciality),
            // and the other fields here.
        ];
    }
}

最后,您可以在控制器中执行以下操作:

use App\Http\Resources\User as UserResource;

function YourMethod() {
    $models = $this->model->where("id_user", "=", $user_id)
                             ->with('typeSpeciality')
                             ->get();
    return UserResource::collection($models);
}

答案 2 :(得分:0)

从模型做出json响应的最佳方法是使用雄辩的资源。看一下the laravel docs of eloquent resources。他们还解释了如何向资源添加关系。