Using accessor to select one result of a related table

Posted 2 months ago by Penaf

Having this:

class Especie extends Model

    protected $table = 'especies';

    public function multimedia()
        return $this->belongsToMany('App\Multimedia', 'especie_multimedia', 'especie_id', 'multimedia_id')->withPivot('isCover');

Is it possible to do somekind of accessor that gives me the multimedia_id from the pivotal table where isCover = 1 ? Or is there a bettter/faster way to do this?

    protected $appends = ['nome_total'];

    public function getCoverAttribute()

    return $multimedia_id;


Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up


Reply to

Use Markdown with GitHub-flavored code blocks.