【发布时间】:2021-05-31 23:01:34
【问题描述】:
我需要完成评分开始的功能。在此代码中,两个模型使用 Post 模型和 PostRate 模型。 Post Model 具有 id、title、description 三个属性 PostRate 有 id、post_id、rate。 我已经制作了 livewire 组件 Rate 和 rate
的资源<?php
namespace App\Http\Livewire;
use App\Models\Post;
use App\Models\PostRate;
use Livewire\Component;
class Rate extends Component
{
public $currentRating=false;
public $postId;
public function render()
{
$post = Post::withCount('rates')->latest('id')->first();
$this->postId = $post->id;
return view('livewire.rate', compact('post'));
}
public function rate($rating)
{
PostRate::create(['post_id'=>$this->postId,'rate'=>$rating]);
$this->currentRating = true;
}
}
资源率
<div>
<h2 class="text-3xl">{{ $post->title }}</h2>
<p>{{ $post->description }}</p>
<div class="mt-4 mb-4">
Current rating: <b>2.83 / 5 ({{ $post->rates_count }} votes)</b>
</div>
<h3 class="text-2xl">Rate the post</h3>
<div class="flex">
<img src="http://demo-star-rating.livewirekit.com/img/star-active.png" width="30">
<a wire:click.prevent="rate(1)" href="#"><img src="http://demo-star-rating.livewirekit.com/img/star-inactive.png" width="30"></a>
<a wire:click.prevent="rate(2)" href="#"><img src="http://demo-star-rating.livewirekit.com/img/star-inactive.png" width="30"></a>
<a wire:click.prevent="rate(3)" href="#"><img src="http://demo-star-rating.livewirekit.com/img/star-inactive.png" width="30"></a>
<a wire:click.prevent="rate(4)" href="#"><img src="http://demo-star-rating.livewirekit.com/img/star-inactive.png" width="30"></a>
<a wire:click.prevent="rate(5)" href="#"><img src="http://demo-star-rating.livewirekit.com/img/star-inactive.png" width="30"></a>
</div>
<div>
You rated: 4 / 5
</div>
</div>
【问题讨论】:
-
问题是……
标签: php laravel laravel-livewire