Search code examples
phplaravelmodel-view-controllerlaravel-livewire

Did i confuse the integer data type with something else?


I dont know how to deal with the error of unsupported operand types: int + App\Models\Rating but the calculation works just correctly. It pinpoints the error to the public function rate starting from this currentRating = round. Im confused because it's new to me in terms of the language so it's big blur.

     <div class="w-full space-y-5">
         <p class="font-medium text-blue-500 uppercase">
             Rating: <strong>{{ $currentRating }}</strong> ⭐ 
         </p>
     </div>
<?php

namespace App\Http\Livewire;

use Livewire\Component;
use App\Models\Rating;

class MovieRatings extends Component
{
    public $rating;
    public $ratings;
    public $currentRating = '-';
    public $comment;
    public $currentId;
    public $movie;
    public $hideForm;

    protected $rules = [
        'rating' => ['required', 'in:1,2,3,4,5'],
        'comment' => 'required',

    ];

    public function mount() 
    {
        if(auth()->user()){
            $rating = Rating::where('user_id', auth()->user()->id)->where('movie_id', $this->movie->id)->first();
            if (!empty($rating)) {
                $this->rating  = $rating->rating;
                $this->comment = $rating->comment;
                $this->currentId = $rating->id;
            }
            $this->ratings = Rating::where('movie_id', $this->movie->id)->get();
            if ($this->ratings->count()) {
            $this->currentRating = round($this->ratings->sum('rating') / $this->ratings->count(), 2) . ' / 5 (' . $this->ratings->count() . ' votes)';
            }
        }
        return view('livewire.movie-ratings');
    }

    public function rate() 
    {
        $rating = Rating::where('user_id', auth()->user()->id)->where('movie_id', $this->movie->id)->first();
        $this->validate();
        if (!empty($rating)) {
            $rating->user_id = auth()->user()->id;
            $rating->movie_id = $this->movie->id;
            $rating->rating = $this->rating;
            $rating->comment = $this->comment;
            $rating->status = 1;
            try {
                $rating->update();
                $this->currentRating = round(($this->ratings->sum('rating') + $rating) / ($this->ratings->count() + 1), 2);
            } catch (\Throwable $th) {
                throw $th;
            }
            session()->flash('message', 'Success!');
        } else {
            $rating = new Rating;
            $rating->user_id = auth()->user()->id;
            $rating->movie_id = $this->movie->id;
            $rating->rating = $this->rating;
            $rating->comment = $this->comment;
            $rating->status = 1;
            
            try {
                $rating->save();
                $this->currentRating = round(($this->ratings->sum('rating') + $rating) / ($this->ratings->count() + 1), 2) . ' / 5 (' . ($this->ratings->count() + 1) . ' votes)';

            } catch (\Throwable $th) {
                throw $th;
            }
            $this->hideForm = true;
        }
    }
}

Solution

  • You probably meant to write + $rating->rating instead of + $rating.

    $rating is an object, not an integer.