-3

I'm trying to change the look of a button based on whether the user has liked an image by using 1 class if he has and another class if he hasn't but I'm getting a parse error which I can not find. Of course the reason could be that I'm trying to do something that can't be done like that.

<a href='#' class='vote {{ Auth::user()->votes()->where("image_id", $image->id)->first() ? Auth::user()->votes()->where("image_id", $image->id)->first()->like == 1 ? "liked" : "like" }}' id='{{$image->id}}'></a>
Onyx
  • 5,186
  • 8
  • 39
  • 86
  • 1
    What makes you think the error is in the code you have posted above? What's the full error message? – elixenide May 21 '18 at 04:23
  • 1
    Pro Tip Break this fancy code into multiple line so you'll get the error along with line number. – Saad Suri May 21 '18 at 04:25
  • 5
    You are using `condition ? condition ? val1 : val2`? :D – vietanhyt May 21 '18 at 04:26
  • And your ternary operator is absolutely wrong – Saad Suri May 21 '18 at 04:27
  • The error happened after writing this piece of code, if I remove it everything is dandy. – Onyx May 21 '18 at 04:27
  • Stacking ternary statements is just asking for trouble ~ http://php.net/manual/language.operators.comparison.php#example-104 – Phil May 21 '18 at 04:30
  • If I'm right, your logic is check if logged in user `like`-ed (via `votes` relationship) the image or not, then display corresponding word. You can do that by: `Auth::user()->votes()->where("image_id", $image->id)->first() ? (Auth::user()->votes()->where("image_id", $image->id)->first()->like == 1 ? "liked" : "like") : "like"`. – vietanhyt May 21 '18 at 04:32
  • The most important thing is not make a piece of code run, but run well with improvement. Please process complex / long user logic before passing them to view. – vietanhyt May 21 '18 at 04:35
  • 1
    You should rethink this code. On top of this being an ugly use of nesting ternaries, you're executing 2 SQL queries for same data here (when the image is found). – DevK May 21 '18 at 04:35
  • Auth is session not Model – Niklesh Raut May 21 '18 at 04:54
  • @C2486 if you're addressing my comment, I didn't mean fetching the user twice. They're calling the votes relationship *query* twice `->votes()`. Not a preloaded property `->votes` – DevK May 21 '18 at 05:01

3 Answers3

1

Try this:-

<a href='#' class='vote {{ ( auth()->user()->votes()->whereImageId($image->id)->first() && auth()->user()->votes()->whereImageId($image->id)->first()->vote == 1 ) ? "liked" : "like" }}' id='{{$image->id}}'></a>
Rajesh kumar
  • 188
  • 7
  • Paste as it is, and it will work for you. I'm damn sure about it :) – Rajesh kumar May 21 '18 at 05:00
  • After pasting this, I no longer get the error, however, my html class doesn't change after a refresh even though the database record shows that the user has liked it. – Onyx May 21 '18 at 05:01
  • I've added your second condition with AND parameter if record found and that have like value 1, then 'liked' class will be applied there. May be you're missing something. – Rajesh kumar May 21 '18 at 05:04
  • If you want to use your code just at last else statement that also will work. But i created it this line with optimized. – Rajesh kumar May 21 '18 at 05:05
  • auth()->user()->votes()->whereImageId($image->id)->first() What's retrurn by this? – Rajesh kumar May 21 '18 at 05:07
  • {"id":14,"user_id":5,"image_id":6,"vote":1,"created_at":"2018-05-21 05:03:08","updated_at":"2018-05-21 05:03:08"} this is the vote from my votes table, now since vote column is 1 it means the image has been liked. – Onyx May 21 '18 at 05:12
  • Hey @Bobimaru , Then why you're putting like in query? You should do it like this:- – Rajesh kumar May 21 '18 at 05:40
  • Replace after && paramter with this auth()->user()->votes()->whereImageId($image->id)->first()->vote And please let me know, if it's not work. – Rajesh kumar May 21 '18 at 05:41
  • Oh my... I didn't realize I've been putting like instead of vote in the query. This works now, thanks a lot. You can put it in an answer so I can select it as solution it if you want. – Onyx May 21 '18 at 05:49
0

try this

 <a href='#' class='vote {{ Auth::user()->votes()->where("image_id", $image->id)->first() ? (Auth::user()->votes()->where("image_id", $image->id)->first()->like == 1 ? "liked" : "like") : '' }}' id='{{$image->id}}'></a>

You need to wrap with () after ? because you make 2 if statement.

ssuhat
  • 7,387
  • 18
  • 61
  • 116
0

I am not sure about your code but it might be helpful for you because you missed the else condition in ternary operator.

<a href='#' class='vote {{ Auth::user()->votes()->where("image_id", $image->id)->first() ? (Auth::user()->votes()->where("image_id", $image->id)->first()->like == 1 ? "liked" : "like" }}):"" ' id='{{$image->id}}'></a>