Skip to content

Can i show in Blade #10

Discussion options

You must be logged in to vote

Hi @mchisnerman3 !
Sure, you can do that in many ways I think. A solution I came up with is something like this:

$model
  ->likers()
  ->whereKey($user->getKey())
  ->exists();

$model
  ->likers()
  ->whereKey(auth()->id())
  ->exists();

Alternatively, you could implement an accessor in your markable model:

public function getHasLikeAttribute()
{
  return $this
    ->likers()
    ->whereKey(auth()->id())
    ->exists();
}

and use it in your blade component:

$model->has_like;

Beware: using the accessor will lead you to the N+1 problem when working with multiple models at once.
In that case, you could easily append the has_like attribute while executing the query:

$models = Model::query()
…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by riccardodallavia
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants