我是否将整数数据类型与其他类型混淆了?
Did i confuse the integer data type with something else?
我不知道如何处理不支持的操作数类型的错误:int + App\Models\Rating 但计算正确。它将错误定位到从此 currentRating = round 开始的 public 函数速率。我很困惑,因为它对我来说是新的语言,所以它很模糊。
<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;
}
}
}
您可能打算写 + $rating->rating
而不是 + $rating
。
$rating
是对象,不是整数。
我不知道如何处理不支持的操作数类型的错误:int + App\Models\Rating 但计算正确。它将错误定位到从此 currentRating = round 开始的 public 函数速率。我很困惑,因为它对我来说是新的语言,所以它很模糊。
<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;
}
}
}
您可能打算写 + $rating->rating
而不是 + $rating
。
$rating
是对象,不是整数。