mardi 6 août 2019

Custom attribute containing whether or not there's a relation between models

I have two models: User and Event.

Both have a polymorphic many to many relationship to each other.

Is there any way when I do Event::all()

to $appends a getJoinedAttribute() that returns true if the Auth::user() has a relation to each Event in the list?

    public function getJoinedAttribute() {
        return $this->whereHas('users', function($query) {
           $query->where('users.id', Auth::user()->id);
        })->count() > 0 ? true : false;

I guess I need something similar to this.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire