Penaf

Using accessor to select one result of a related table

Posted 8 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.

Reply to

Use Markdown with GitHub-flavored code blocks.