Laravel with() method, get all records from it

I have a question related to getting some records or data from databases on Laravel. So I have this relationship in my Models. For my VoteChoiceModel:

public function Theme() {
    return $this->hasMany('App\Models\VoteThemeModel', 'vote_themes_id', 'id');
}

And for my VoteThemeModel:

public function Choice() {
    return $this->belongsTo('App\Models\VoteChoiceModel', 'id', 'vote_themes_id');
}

then at my controller I have this code:

public function getParticipant() {
    $data = VoteThemeModel::select('*')->with('Choice')->get();
    return $data;
}

So in short if I run this I'll get the reply of json like this:

"data": [
    {
        "name": "Theme-1",
        "banner": "Banner",
        "choice": {
            "id": 4,
            "name": "Doe Jhon",
            "icon": "PICT"
        }
    },
    {
        "name": "Theme-2",
        "banner": "-1620119938-Screenshot (1).png",
        "choice": {
            "id": 8,
            "name": "JhonDOO",
            "icon": "ICONIC"
        }
    }
    ]

As you can see the choice is JSON only return one data/records (In Theme-1 only Doe Jhon and Theme-2 is JhonDOO) while actually, it contains more than one data/records. So in that ->choice, I want to return all data that related to it.

Can someone help me to figure this out? I'm not so sure that I may have some mistakes in the relationship.

Thanks!!

==========================================

UPDATE:

As I tried to swap both models eloquent method:

//VoteChoiceModel
public function Theme() {
    return $this->belongsTo('App\Models\VoteThemeModel', 'vote_themes_id', 'id');

}

//VoteThemeModel
public function Choice() {
    return $this->hasMany('App\Models\VoteChoiceModel', 'id', 'vote_themes_id');
}

I got this JSON reply:

{
    "status": true,
    "message": "All Participant",
    "data": [
        {
            "id": 1,
            "name": "Jhon Doe",
            "icon": "ICON",
            "theme": {
                "name": "Theme-1",
                "banner": "Banner"
            }
        },
        {
            "id": 4,
            "name": "Doe Jhon",
            "icon": "PICT",
            "theme": {
                "name": "Theme-1",
                "banner": "Banner"
            }
        },
        {
            "id": 8,
            "name": "JhonDOO",
            "icon": "ICONIC",
            "theme": {
                "name": "Theme-2",
                "banner": "-1620119938-Screenshot (1).png"
            }
        }
    ]
}

All record of choice is there, with the theme! it works! but the problem is that I don't want to return every theme on every choice. So the format should be theme on the top of the ->data JSON.



Read more here: https://stackoverflow.com/questions/67395556/laravel-with-method-get-all-records-from-it

Content Attribution

This content was originally published by Ivan Yunus Kolotosa at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: