AwadGorg
1 week ago

Finding rows in database table using laravel Eloquent

Posted 1 week ago by AwadGorg

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

Please sign in or create an account to participate in this conversation.