Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Destiel's avatar

Laravel multiple id foreign problem

First of all I apologize for my bad English.

The database structure is as follows:

id, title, genres_id,poster

below is sample table data

1|Baslik|2,4,6|img path

Three pieces of data in the 2,4,6 genres table

I'm trying to pull the required data from the 'genres' table, but I couldn't

0 likes
8 replies
Destiel's avatar

i can't pull it. If it was the only data I could pull with belongsTo.

thoughtControl's avatar

Again, what do you mean "pull it"? Using Eloquent, query builder??

Destiel's avatar

I can get the lines in the database as $series->title, my aim is to be able to retrieve the data from the array as $series->getGenres()

Destiel's avatar

I want to have codes suitable for mvc, it would not be correct to do database query in view.

Of course there is a way to reach with the model, maybe I have to store it differently

Destiel's avatar

I solved it as follows:

public function getGenres()
    {
      foreach (explode(',',$this->genres_id) as $id) {
        $genres[] = Genres::find($id);
      }
      return $genres;
    }
MichalOravec's avatar

@destiel It could be like this

return Genres::whereIn('id', explode(',', $this->genres_id))->get();
1 like

Please or to participate in this conversation.