Home > Mobile >  How to show many to many relationship data in laravel8
How to show many to many relationship data in laravel8

Time:08-30

I have many to many relationship data into the database using below code fetching data in controller :

public function index(){
        $data = Singer::with('songs')->get();
        return view('singers_index')->with('singer_data',$data);
    } 

data getting as below format :

[{"id":1,"name":"Praful","created_at":"2022-08-29T12:51:03.000000Z","updated_at":"2022-08-29T12:51:03.000000Z","songs":[{"id":1,"title":"Chocolate karne laga","created_at":"2022-08-29T12:45:15.000000Z","updated_at":"2022-08-29T12:45:15.000000Z","pivot":{"singer_id":1,"song_id":1}},{"id":2,"title":"Chocolate Dance laga","created_at":"2022-08-29T12:48:12.000000Z","updated_at":"2022-08-29T12:48:12.000000Z","pivot":{"singer_id":1,"song_id":2}}]},{"id":2,"name":"Mayuresh","created_at":"2022-08-29T14:42:40.000000Z","updated_at":"2022-08-29T14:42:40.000000Z","songs":[{"id":3,"title":"Baby ko base pasand hai","created_at":"2022-08-29T14:42:07.000000Z","updated_at":"2022-08-29T14:42:07.000000Z","pivot":{"singer_id":2,"song_id":3}}]}]

I am not getting how to set multiple songs into the table using comma seperator

 @foreach($singer_data as $all_data)
<tr>
    <td>{{$all_data->name}}</td>
    <td>{{$all_data->user->title}}</td>
</tr>
@endforeach

Not getting how to add that songs with comma seperator into the view.

I have added belongsToMany relationship with both models i.e. Song and Singer

class Singer extends Model
{
    use HasFactory;
    public function songs(){
        return $this->belongsToMany(Song::class,'singer_songs');
    }
}

class Song extends Model
{
    use HasFactory;
    public function singer(){
        return $this->belongsToMany(Singer::class,'singer_songs');
    }
}

If anyone have idea how to display into the view with comma seperator. Please help me in this

CodePudding user response:

There are various methods for pulling data and displaying it, such as pluck() and implode():

public function index(){
  return view('singers_index', [
    'singers' => Singer::with('songs')->get()
  ]);
} 

Then in your .blade.php file:

@foreach($singers as $singer)
  <td>{{ $singer->songs->pluck('title')->implode(', ') }}</td>
  <!-- OR -->
  <td>{{ $singer->songs->implode('title', ', ') }}</td>
  <td>{{ $singer->user ? $singer->user->name : 'No User' }}</td>
@endforeach

The ->pluck('title') returns a Collection of Song Titles, like:

collect(['Chocolate Dance laga', 'Baby ko base pasand hai'])

And ->implode(', ') returns them as a comma-separated string:

Chocolate Dance laga, Baby ko base pasand hai

->implode('title', ', ') is alternative syntax, and skips the pluck() method, but outputs the same comma-separated string.

Documentation:

->pluck() method

->implode() method

CodePudding user response:

you can use laravel accesser. I think it's the best clean way

class Singer extends Model
{
  public function getAllSongsAttribute()
    {
        return $this->songs->implode("title", ",");
    }
}

now use this attribute on singer model:

$singer->all_songs ;
  • Related