mercredi 9 octobre 2019

How to fetch data using with via object in Laravel eloquent?

Actually, I want to fetch data using Model object not through Model facade like mention below.

$user = $this->getGuard()->user();

Above user is current logged user which is

        "id": 6,
        "name": "kuser",
        "email": "kuser@gmail.com",
        "phone": "03345154067",
        "is_subscribed": 0,
        "subscription_date": null

But when I try to fetch same $user object with its related model like cards, by execute $user->with(['cards'])->first(); than it gives first record of user table with cards like mentioned below.

        "id": 1,
        "name": "admin",
        "email": "admin@gmail.com",
        "phone": "03056494616",
        "is_subscribed": 1,
        "subscription_date": "2019-10-08",
        "cards": []

Above mentioned record which is user object. It is first record of my user table.

Actually, I am expecting.

      "id": 6,
        "name": "kuser",
        "email": "kuser@gmail.com",
        "phone": "03345154067",
        "is_subscribed": 0,
        "subscription_date": null

with its related model cards like

      "id": 6,
        "name": "kuser",
        "email": "kuser@gmail.com",
        "phone": "03345154067",
        "is_subscribed": 0,
        "subscription_date": null
        "cards":[] // array of cards those belongs to it.


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire