API that returns JSON

Posted 7 months ago by nyel-dev

I have this Resort model

class Resort extends Model
{
    protected $hidden = [
        'owner_id', 'created_at', 'updated_at'
    ];

    public function getInformation() {
        return $this->hasOne(ResortInformation::class);
    }

    public function getPictures() {
        return $this->hasMany(ResortGallery::class);
    }
}

and this controller

class ResortController extends Controller 
{
    use App\Resort;

    public function getResortDetails($id) {
        $resort = Resort::find($id);
        $resort->information = $resort->getInformation;
        $resort->images = $resort->getPictures;

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

How do I append my getInformation and getPictures data into the $resort object?

everytime I use $resort->getInformation or $resort->getPictures, the json is duplicating it

the example is like this

    [
        {
            "id": 1,
            "name": "Test Resort 1",
            "information": {
                "max_people": 10,
                "pools": 1,
            },
            "images": [
                {
                    "image_path": "uploads/1/2018-11/background.jpg"
                },
                {
                    "image_path": "uploads/1/2018-11/1.jpg"
                },
            ],
            "get_information": {
                "max_people": 10,
                "pools": 1,
            },
            "get_pictures": [
                {
                    "image_path": "uploads/1/2018-11/background.jpg"
                },
                {
                    "image_path": "uploads/1/2018-11/1.jpg"
                }
            ]
        }
    ]

this is the exact response the behavior is adding "get_information" and "get_pictures" even though I have not stated it to show

Please sign in or create an account to participate in this conversation.