Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eager load $article->isFavorited() #32

Open
Ahmad-Fathy opened this issue Jul 21, 2022 · 1 comment
Open

Eager load $article->isFavorited() #32

Ahmad-Fathy opened this issue Jul 21, 2022 · 1 comment

Comments

@Ahmad-Fathy
Copy link

Ahmad-Fathy commented Jul 21, 2022

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
@Saifallak
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants