Kevin Houde
Kevin Houde

Reputation: 319

Laravel 5.5. Include relation table in json

I try to include join table in my json return with laravel 5.5 with dingo.

I have this line : return $this->model->where("id_user", "=", $user_id)->with('typeSpeciality')->get();

And I got this result, speciality table is not include

{
    "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
}

speciality table is not include in my json... I want something like this

{
    "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
}

My relationship function

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

What is the best way to do this?

Upvotes: 1

Views: 602

Answers (3)

Maraboc
Maraboc

Reputation: 11093

You can get what you want using the Eloquent: API Resources :

You have to create a User (assuming that the other model is User) and TypeSpeciality resource :

<?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
        ];
    }
}

Then the User resource :

<?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.
        ];
    }
}

Finally in the controller you can do this :

use App\Http\Resources\User as UserResource;

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

Upvotes: 1

rkj
rkj

Reputation: 8307

Change your relationship from hasOne to belongsTo like this

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

Upvotes: 2

Jacob
Jacob

Reputation: 132

The best way to make json response from a model is to use eloquent resources. Take a look at the laravel docs of eloquent resources. They also explain how you can add relations to the resource.

Upvotes: 0

Related Questions