AwadGorg's avatar

Finding rows in database table using laravel Eloquent

Hello, I Have this code

$checkRatings = $ratingTable::select('browsergames_rating.id')->where([['user_id', $userId], ['game_id', $gameId]])->first('id');

and also tried it with

$checkRatings = $ratingTable::select('browsergames_rating.id')->where([['user_id', $userId], ['game_id', $gameId]])->get('id');

I am using it to the first check if the user who is rating the item is rated it before or not and also I want to get the id from the table and both of these are done but when I try to use the id from the code to find the row in the database that the user is tried to rate and update there previews rating

$insertRating = $ratingTable::find($checkRatings);

and when I tried to echo out the $checkRatings var to see what's wrong it turned out that it shows the data retrieved from the database in this format

echo $checkRatings; die();

output

{"id":8}

expected output

5

so I can use the 5 and the five here is the id and find the row and update it part of the code I amusing

/**
    * function to insert ratings from registered users
    **/
    protected function userRating($gameId, $index, $expire, $ratingTable){
    // if the session is set and the user rating is already a member
    $userId = Auth::user()->id;
    $checkRatings = $ratingTable::select('browsergames_rating.id')->where([['user_id', $userId], ['game_id', $gameId]])->first('id');
    echo $checkRatings; die();
    if (!empty($checkRatings)) {
        $insertRating = $ratingTable::find($checkRatings);
        $insertRating->rating = $index;
        $insertRating->game_id = $gameId;
        $insertRating->user_id = $userId;
        $insertRating->save();
        echo "Thank you for rating this game " . $index . " Out Of 5";
    }else{
        $insertRating = new $ratingTable;
        $insertRating->rating = $index;
        $insertRating->user_id = $userId;
        $insertRating->game_id = $gameId;
        $insertRating->save();
        echo "Thank you for rating this game " . $index . " Out Of 5";
    }
    }

i tried to json_encode the $checkRatings but still return the another problem

0 likes
2 replies
bobbybouwmann's avatar
Level 88

It seems that you're pretty new to the framework. I highly recommend you to follow this free series: laravelfromscratch.com

Anyway, let's go back to the code. The first method returns the first result of the query. See it as running a query with the LIMIT 1 option. You're doing the select now twice. In the query itself and also in the first call. There is no need for that.

Try to keep things as simple as possible in the beginning

$ratingTable::where([['user_id', $userId], ['game_id', $gameId]])->first();
2 likes
AwadGorg's avatar

Yes am tottally new to frameworks I have tuns of tutorials am watching but i want to build me some application to keep what i already now about laravel and also thanks a lot for your help I removed the select from my query

$checkRatings = $ratingTable::where([['user_id', $userId], ['game_id', $gameId]])->first();

and changed the find to

$insertRating = $ratingTable::find($checkRatings->id);

Please or to participate in this conversation.