Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
607 views
in Technique[技术] by (71.8m points)

php - Getting Duplicate Data on Response On Laravel Eloquent

I am new in laravel in php. So it might be very silly mistake. I have song table and song categories table. I am trying to fetch all category with their respective songs. I have implemented larvel eloquent one to many relationship between song category and song.

Here is my code of fetching data:

 public function getSongCategoriesWithSongs(){

     $json_array = array();
    $song_categories = SongCategory::all();

    foreach ($song_categories as $item) {
        # code...
        $json = [];
        $json['category'] = $item;
        $json['songs'] = $item->songs;
        array_push($json_array,$json);
    }

    return $json_array;
}

Here is response:

[{
    "category": {
        "id": 1,
        "title": "Rock",
        "created_at": "2020-12-20T02:58:32.000000Z",
        "updated_at": "2020-12-20T02:58:32.000000Z",
        "songs": [{
            "id": 1,
            "title": "Mere Mehboob",
            "thumbnail": "https://static.toiimg.com/photo/msid-71407401/71407401.jpg?108311",
            "song_category_id": 1,
            "stream_link": "https://2u039f-a.akamaihd.net/downloads/ringtones/files/mp3/mere-mehboob-qayamat-hogi-52150.mp3",
            "created_at": "2020-12-20T13:26:30.000000Z",
            "updated_at": "2020-12-20T13:26:30.000000Z"
        }, {
            "id": 2,
            "title": " Taaron Ke Shehar",
            "thumbnail": "https://static.toiimg.com/photo/msid-71407401/71407401.jpg?108311",
            "song_category_id": 1,
            "stream_link": "https://newmp3ringtones.net/assets/sass/Ringtones/TaaronKeSheharRingtoneByNehaKakkarJubinNautiyal2145436126.mp3",
            "created_at": null,
            "updated_at": null
        }, {
            "id": 3,
            "title": "Bewafa Tera Masoom Chehra",
            "thumbnail": "https://static.toiimg.com/photo/msid-71407401/71407401.jpg?108311",
            "song_category_id": 1,
            "stream_link": "https://newmp3ringtones.net/assets/sass/Ringtones/BewafaTeraMasoomChehraRingtoneByJubinNautiyal352778308.mp3",
            "created_at": null,
            "updated_at": null
        }]
    }
}, {
    "songs": [{
        "id": 1,
        "title": "Mere Mehboob",
        "thumbnail": "https://static.toiimg.com/photo/msid-71407401/71407401.jpg?108311",
        "song_category_id": 1,
        "stream_link": "https://2u039f-a.akamaihd.net/downloads/ringtones/files/mp3/mere-mehboob-qayamat-hogi-52150.mp3",
        "created_at": "2020-12-20T13:26:30.000000Z",
        "updated_at": "2020-12-20T13:26:30.000000Z"
    }, {
        "id": 2,
        "title": " Taaron Ke Shehar",
        "thumbnail": "https://static.toiimg.com/photo/msid-71407401/71407401.jpg?108311",
        "song_category_id": 1,
        "stream_link": "https://newmp3ringtones.net/assets/sass/Ringtones/TaaronKeSheharRingtoneByNehaKakkarJubinNautiyal2145436126.mp3",
        "created_at": null,
        "updated_at": null
    }, {
        "id": 3,
        "title": "Bewafa Tera Masoom Chehra",
        "thumbnail": "https://static.toiimg.com/photo/msid-71407401/71407401.jpg?108311",
        "song_category_id": 1,
        "stream_link": "https://newmp3ringtones.net/assets/sass/Ringtones/BewafaTeraMasoomChehraRingtoneByJubinNautiyal352778308.mp3",
        "created_at": null,
        "updated_at": null
    }]
}, {
    "category": {
        "id": 2,
        "title": "Soft",
        "created_at": null,
        "updated_at": null,
        "songs": []
    }
}, {
    "songs": []
}]

As you can see songs get repeated.

UPDATE

Solved using eager loading

 public function getSongCategoriesWithSongs(){
    return SongCategory::with('songs')->get();
}

But don't know why the foreach method not working.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try this code

public function getSongCategoriesWithSongs(){

 $json_array = array();
$song_categories = SongCategory::all();

foreach ($song_categories as $item) {
    $json_array[] = ['category' => $item, 'songs' => $item->songs] ;
}
return $json_array;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...