Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

opheliadesign's avatar

@ufodisko Voting is working on my pull request but there's some weird behavior related to the jQuery library you're using. I tried figuring it out based on their docs but gave up. Basically, if a vote is already upvoted and you click on Upvote again, it switches to 0 without Upvote or Downvote highlighted. Similarly, if a post is already upvoted, clicking on downvote makes it go from 1 to -1 instead of 0 (assuming yours is the only vote).

Hopefully the Eloquent stuff has explained a few things for you, along with how I usually approach Ajax with Laravel. I was able to:

  1. Persist the class of the upvote/downvote, even though there are the other bugs mentioned above
  2. Prevent multiple votes in the table from the same user
  3. Eager load a Subreddit's Posts and their related Votes, setting the vote counter and class for the upvote/downvote buttons.
2 likes
ufodisko's avatar

@opheliadesign This is brilliant man, you've cracked it! I need some time to look at what you've done and read a bit more on eager loading and a bunch of other stuff.

I cloned your fork and tested it, everything works except 1 thing. If a post was already upvoted and I clicked on upvote again (to cancel my original vote or set it to 0), nothing happens. The vote is still the same in the database.

However if the post was already upvoted and I clicked on 'downvote', the value in the table row will be updated from 1 to -1 and vice versa. Which is what's needed here, no?

opheliadesign's avatar

@ufodisko Awesome, dude! Glad I could help, had fun hacking on that for a while. And yes, please read up on Eloquent some more. Honestly, I started with Laravel 3.2 and only recently began digging into everything that Eloquent can do - pretty amazing. The lessons and people here at Laracasts have helped me quite a bit.

So, yeah, you want the vote to be either +1 or -1 - we only have one vote per user per post. If someone hasn't voted, neither the upvote or downvote arrows will have their active class. Once the user votes, we want to hang on to that vote even if they change their mind later on. If they downvote initially and then upvote, we still want to show that they upvoted even if the number of votes is 0. We're calculating the number of votes by getting the sum of the value column. Make sense?

Again, I couldn't figure out jquery.upvote in the time I had this evening, but you need to play around with that a bit more to prevent the weird behavior I mentioned in my previous post. If a user already upvoted a post and click upvote again, nothing should happen. If they already upvoted and then downvoted, the counter should decrement by one and the downvote arrow should be highlighted (the library seems to handle that part automatically).

ufodisko's avatar

@opheliadesign Yes it makes sense. But if the user upvoted a post, that adds a value of 1 in the table to that post_id. If he decides to click on the upvote button again, it should set it to 0 or remove the row from the table completely. We don't wanna include a vote that was canceled in the sum we're returning. Does my reasoning make sense?

Also, do you think it's a good idea to use that jquery.upvote.js plugin or should I have went with manually creating the buttons and all? I kind of picked it because it handles some things automatically as you've said, but I am not aware of any other disadvantages this might bring.

opheliadesign's avatar

@ufodisko The way Stack Exchange sites work, if you have upvoted or downvoted something and then repeat that same vote, nothing happens. So, if I upvote an answer and then upvote it again, my vote doesn't change to 0 - it stays the same as it was. It will only change if I downvote, at which point my downvote will persist and it will show that I downvoted that answer the next time I view it.

So, there's really no "canceling" a vote - you either haven't voted, you upvoted, or you downvoted. The post's score up or down is based on the sum of the votes.value column.

Actually, I really like jquery.upvote, I'll use it myself. Just didn't have time to really dig into how it works.

And dude, there's a possum walking under my deck as I sit here and type this.. ahh, the country. :P

ufodisko's avatar

@opheliadesign I get that, I really do. But that's not what I'm going for. I want it to be much like reddit. If the user to decided to cancel his vote (by upvoting again) it should really cancel his vote, otherwise the arrows are gonna show that no vote has been made but in the row in the database of the previous vote is still there and counted.

I think I need to add a third option which is to remove the original vote in case of cancellation.

And how did that possum get under your desk? That's a story I really wanna hear. hehe

opheliadesign's avatar

@ufodisko Ahhh okay, gotcha. In that case, you'd need to revise the code within the store() method to see if the user has already voted and, if so, adjust accordingly. So, if they previously upvoted and then downvoted, just delete it - and visa versa. Try to figure that out on your own and we'll catch up tomorrow ;) It's a shame, though, because the Eloquent query I used was pretty sweet!

And you read wrong, possum under my DECK.. during the warmer months my home office is on my front porch, overlooking a corn field in the Northern Neck of Virginia. :) And it was a big sucker, by the way.. I thought it was a dog at first lol

ufodisko's avatar

@opheliadesign I will try to figure it out myself, thank you :)

In the comments of the store() method, you said I need to add some validation on VoteRequest. That is true but what kind of validation I can have there? The input is not required, and it's just a button with no user inputs.

opheliadesign's avatar

@ufodisko argh I couldn't resist, just did a new pull request. Don't look at it if you want to do it yourself (which you should). But it works if you use it ;)

Actually there really isn't any validation that you need to do. Instead I'd delete that Form Request class and just use use Illuminate\Http\Request; in the Store method.

1 like
ufodisko's avatar

@opheliadesign oh man you're a rock star!!! I just saw it and merged it. Haven't tested it though.

Thank you so much man!!!

Would you like me to add you as a contributor? I think you deserve it more than I do lol. I am going long with this project and yes it's a big one but that's not a bad way to learn so I'm gonna stick with it. It can turn out to be a fun little project which can benefit the entire laravel community.

And yes, I will remove the usage of Request.

1 like
opheliadesign's avatar

@ufodisko just the other day I was thinking to myself that I should contribute to a project, so that would be great! But I really don't want to steal your thunder so I'll step back unless you want me to contribute some code. After all, as you have stated, the goal is for you to learn the framework.

Follow me on Twitter and PM me, @OpheliaDesign and @BenjaminMcMahan :)

The possum has gone off and so shall I. Goodnight, been fun!

ufodisko's avatar

@opheliadesign No, on the contrary, I'd love to have you join and contribute. I can only learn so much on my own, so having a better and fresh perspective is an advantage.

I do not use twitter, sorry.

I will follow you on Github and add you as a contributor and we can discuss our future communication methods and strategy tomorrow :)

Goodnight for now and thank you very much man.

1 like
talha08's avatar

You Can use without joining, just use a foreach .

      public function index(Request $request, $id)
        {
            $items = ReportGlAssignment::where('shop_id', $id)->paginate(10);
            
            foreach ($items as $item ){
              $bill =  BillingAssignment::where('id',$item->billing_assignment_id )->pluck('assignment');
              $item['billing_assignment_id'] = $bill;
            }
    
            return response()->json($items);
        }

It will show the name of your foreign key data,

    {
    id: 7,
    designation: "​Food Service",
    income_gl: "333",
    expense_gl: "333",
    billing_assignment_id: "bgjjnhkj",
    shop_id: 12,
    created_at: "2017-08-21 07:26:48",
    updated_at: "2017-08-23 09:35:54"
    },

Previous

Please or to participate in this conversation.