Здравствуйте, у нас есть три сущности: инструменты, композиторы и треки.
Инструменты и композиторы связаны между собой многими.
Когда вы нажимаете на инструмент (id), я бы Мне нравится показывать количество композиторов, которые написали несколько «треков» для этого инструмента.
Я использую этот код, но думаю, что это не совсем понятно почему, и я не очень хорошо понял Eloquent и его методы в laravel, вы можете мне помочь? PS это работает, но я не заполняю хорошо ..
мой контроллер инструментов:
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']));
}
мой взгляд:
@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
Спасибо
изменить модель трека
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');
}
}
модель инструмента
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 модель
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);
}
}
модель сводного стола
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);
}
}
Извините, я очень новичок ie