laravel-favorite icon indicating copy to clipboard operation
laravel-favorite copied to clipboard

Eager load $article->isFavorited()

Open Ahmad-Fathy opened this issue 1 year ago • 1 comments

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!

  • Laravel 9
  • Livewire

Ahmad-Fathy avatar Jul 21 '22 08:07 Ahmad-Fathy

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

Saifallak avatar Sep 18 '22 14:09 Saifallak