1
votes

I want to get relationship data into json using Resource in laravel 5.6

When I query, I get response.data.created_by as an object. (first data marked in box) (I need this kind of functionality using API Resources)
But with API Resources it is only showing id and not "created_by" object in response.response.data.created_by. (second data marked in box)

*The data difference is marked inside box.
*The data is fetched using eager fetch.

url: http://localhost:8000/api/product/unit
Response:


    {
        "data": [
            {
                "id": 1,
                "unit": "Meter",
                "symbol": "m",
                "decimal": 1,
                +----------------------------------------------------------------------------------+
                |"created_by": {                                                                   |
                |    "id": 1,                                                                      |
                |    "name": "Admin",                                                              |
                |    "email": "[email protected]",                                                   |
                |    "api_token": "$2y$10$.c7eJGS6x/C8JN9Hd.Qc1OgPUS8txMDuIHjZNBRRlHQVGrYbJcC5u",  |
                |    "created_at": "2018-05-09 15:45:59",                                          |
                |    "updated_at": "2018-06-08 15:38:41"                                           |
                |},                                                                                |
                +----------------------------------------------------------------------------------+
                "updated_by": {
                    "id": 1,
                    "name": "Admin",
                    "email": "[email protected]",
                    "api_token": "$2y$10$.c7eJGS6x/C8JN9Hd.Qc1OgPUS8txMDuIHjZNBRRlHQVGrYbJcC5u",
                    "created_at": "2018-05-09 15:45:59",
                    "updated_at": "2018-06-08 15:38:41"
                },
                "created_at": "2018-06-19 00:38:54",
                "updated_at": "2018-06-19 20:00:16"
            }
        ],
        "resource": {
            "data": [
                {
                    "id": 1,
                    "unit": "Meter",
                    "symbol": "m",
                    "decimal": 1,
                    +----------------+
                    |"createdBy": 1, |
                    +----------------+
                    "updatedBy": 1,
                    "createdAt": {
                        "date": "2018-06-19 00:38:54.000000",
                        "timezone_type": 3,
                        "timezone": "Asia/Kolkata"
                    },
                    "updatedAt": {
                        "date": "2018-06-19 20:00:16.000000",
                        "timezone_type": 3,
                        "timezone": "Asia/Kolkata"
                    }
                }
            ]
        }
    }

UnitController.php:


    namespace App\Http\Controllers\Product;

    use App\Models\Product\Unit;
    use Illuminate\Http\Request;
    use App\Http\Controllers\Controller;
    use Illuminate\Support\Facades\Validator;
    use App\Http\Resources\Product\UnitResourceCollection;
    use App\Http\Resources\Product\UnitResource;
    use Illuminate\Validation\ValidationException;

    class UnitController extends Controller {
        public function index()
        {
            $units = Unit::with(['created_by', 'updated_by'])->get();
            +------------------------------------------------------+
            |return [                                              |
            |    'data' => $units,                                 |
            |    'resource' => new UnitResourceCollection($units)  |
            |];                                                    |
            +------------------------------------------------------+
        }
    }

Unit Model:


    namespace App\Models\Product;
    use Illuminate\Database\Eloquent\Model;
    class Unit extends Model
    {
        public function created_by() {
            return $this->belongsTo('App\User', 'created_by', 'id');
        }

        public function updated_by() {
            return $this->belongsTo('App\User', 'updated_by', 'id');
        }
    }
    
UnitResource.php
<pre>
namespace App\Http\Resources\Product;

use App\Http\Resources\UserResource;
use Illuminate\Http\Resources\Json\JsonResource;

class UnitResource extends JsonResource
{
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'unit' => $this->unit,
            'symbol' => $this->symbol,
            'decimal' => $this->decimal,
            'createdBy' => $this->created_by,
            'updatedBy' => $this->updated_by,
            'createdAt' => $this->created_at,
            'updatedAt' => $this->updated_at
        ];
    }
}

1

1 Answers

1
votes

The problem was in Unit Model: I had to use different method name created_by() than the column name created_by.

After changing following code, it is working:

Unit.php Model:

public function created_by() { -> public function createdby() {

public function updated_by() { -> public function updatedby() {


UnitController.php Controller:

$units = Unit::with(['created_by', 'updated_by'])->get(); -> $units = Unit::with(['createdby', 'updatedby'])->get();


UnitResource.php Resource:

'createdBy' => $this->created_by, -> 'createdBy' =>new UserResource($this->createdby),

'updatedBy' => $this->updated_by, -> 'updatedBy' => new UserResource($this->updatedby),