ChristianKuri / laravel-favorite

Allows Laravel Eloquent models to implement a 'favorite', 'like', 'remember' and 'follow' features.
MIT License
226 stars 48 forks source link

Eager load $article->isFavorited() #32

Open Ahmad-Fathy opened 2 years ago

Ahmad-Fathy commented 2 years ago

Detailed description

Hi there!

When i check if article is favorited by auth user

$article->isFavorited()

the debugbar gives me

Model: App\Models\Article => Relation: ChristianKuri\LaravelFavorite\Models\Favorite - You should add with(ChristianKuri\LaravelFavorite\Models\Favorite) to eager-load this relation.

wire controller

/**
   * The read function.
   * @return array
   */
  public function read(): array
  {
    return [
      'articles' => Article::search($this->search)
        ->with('user')
        ->where('status', true)
        ->latest()
        ->paginate($this->perPage),
    ];
  }

  public function render()
  {
    return view('livewire.front.article-wire', $this->read())->layout('layouts.app');
  }

In Article model i tried

protected $with = ['favorites'];

but it didn't work

How could I eager load that?

Thanks!

Saifallak commented 2 years ago

don't think this is possible to eager load that query.