Home » Php » php – How to return data trough tables Laravel

php – How to return data trough tables Laravel

Posted by: admin February 25, 2020 Leave a comment

Questions:

Hello we have three entities, Instruments, Composers and Tracks.

Instruments and Composers are linked with a many to many relationship.

When you click on a instrument(id) i would like to display the number of composers that have write some “tracks” for that instrument.

I use this code, but i think it’s not the cleanest why, and I didn’t understood well Eloquent and his methods in laravel , can you help me?
p.s it works but I don’t fill okay..

my Instruments controller:

public function getInstrument( Instrument $instrument){
$id_compositori= ComposerInstrument::where('instrument_id', $instrument->id)->pluck('composer_id')->toArray();
$compositori = Composer::whereIn('id',$id_compositori)->get();

return  view('instrumentcomposer',compact(['id_compositori','instrument','compositori']));

}

my view:

@foreach($compositori as $composer)
<div class="col-lg-3 col-md-4 col-6">
  <img class="img-fluid img-thumbnail" src=" https://i.picsum.photos/id/619/200/300.jpg?grayscale" alt="">

<h2>Wrote by  {{ $composer->name }} </h2>

   </div>

@endforeach

Thank you

edit
Track model

class Track extends Model
{
   public function instrument()
   {
       return $this->belongsTo(Instrument::class,'instrument_id','id');
   }

   public function composer()
   {
       return $this->belongsTo(Composer::class,'composer_id','id');
   }
}

Instrument model

class Instrument extends Model
{
   public function composers1()
   {
       return $this->belongstoMany(ComposerInstrument::class);
   }
   public function tracks()
   {
       return $this->hasMany(Track::class);
   }
   public function composers()
   {
       return $this->hasMany(Composer::class);
   }

Composer model

class Composer extends Model
{

   public function instruments()
   {
       return $this->belongstoMany(ComposerInstrument::class);
   }

   public function tracks()
   {
       return $this->hasMany(Track::class);
   }
   public function instruments2()
   {
       return $this->hasMany(Instrument::class);
   }


   }

Pivot Table Model

class ComposerInstrument extends Model
{
   protected $table = 'composer_instrument';
   protected $fillable = ['composer_id','instrument_id'];
   public function instruments()
   {
       return $this->hasMany(Instrument::class);
   }

   public function composers()
   {
       return $this->hasMany(Composer::class);
   }
}

Sorry i’m very newbie

How to&Answers:

The first parameter of belongsToMany() should reference the model that it has that relationship with not the pivot model.

Change the composers() method your in your Instrument model to be:

public function composers()
{
    return $this->belongsToMany(Composer::class);
}

Then your controller code can be:

public function getInstrument(Instrument $instrument)
{
    return  view('instrumentcomposer', compact('instrument'));
}

Lastly, your blade file would be:

@foreach($instrument->composers as $composer)
    <div class="col-lg-3 col-md-4 col-6">
        <img class="img-fluid img-thumbnail" src="https://i.picsum.photos/id/619/200/300.jpg?grayscale" alt="">

        <h2>Wrote by  {{ $composer->name }} </h2>
   </div>
@endforeach