0

I am trying to add a feature to a website built with Laravel. There is a table containing vote numbers and user. I want to get the total points a user has in a certain category. I do not have any PHP or Laravel experience but said I would give this a shot.

$votes1 = UserVotes::select ('select vote from user_votes where feedback_id = ? and feedback_type = 1',  Auth::user()->id);

This should return an object containing the vote amount. I want to interrogate the the object to check if the vote number is above a certain amount and then do something based on that being the case or not.

if vote > 50{
    //do stuff 
}

foreach ($votes1 as $vote1) {
     echo $vote1->vote;
}

The query should return 1. I have verified this by querying the database, so the problem is with my understanding of Laravel or php. What I am doing wrong?

2
  • What is the error you get? Commented Aug 14, 2015 at 11:39
  • No error, just an empty object. Commented Aug 14, 2015 at 11:46

2 Answers 2

2

You don't need to construct your own SQL statement; Eloquent will do that for you.

If your models are set up in the default way, your query would look something like:

$votes = UserVotes::where('feedback_id', Auth::user()->id)
    ->where('feedback_type', 1)
    ->get();

You can then iterate over that as normal.

Additionally, if there is a relationship set up with the user model you could do something like

$votes = Auth::user()->votes()
    ->where('feedback_type', 1)
    ->get();

Check out the documentation here: http://laravel.com/docs/4.2/eloquent

Sign up to request clarification or add additional context in comments.

Comments

2

assuming UsersVotes extends Model, here's how you should do it: UsersVotes::select('vote')->where('feedback_type', 1)->where('feedback_id', Auth::user()->id)-get();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.