jeudi 9 janvier 2020

Laravel hasOne get only one field instead of object

There two models user and also address, which contains country, city and etc. I need to get a list of users with the city, not with the whole address. Relation is oneToOne. The only thing I can get, using select['user_id', 'city']:

    "id": 1,
    "name": "John",
    "city": {
        "user_id": 3,
        "city": "Paris"
    },

but I need:

    "id": 1,
    "name": "John",
    "city": "Paris"

Of course, I can use a loop and do something like $user->city = $user->city->city but maybe there is a better way to solve this problem. Laravel 5.4



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire