samedi 24 août 2019

laravel morph relation access to models data

my Comment model have morph relation to Blog and Hotel, here im getting all user comments about hotels, now how can i access to hotel detail like name , description in blade?

 $hotelComments = Comment::whereHasMorph(
        'commentable',
        Hotel::class,
        )->whereUserId(\Auth::id())->get();

blade:

     @foreach ($hotelComments as $hc)
        <tr>
            <td class=""></td>
            <td class=""></td>

          </tr>
        @endforeach

Blog and Hotel :

 public function comments(){
        return $this->morphMany(Comment::class, "commentable");
    }

Comment Model:

  public function commentable(){
        return $this->morphTo();
    }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire