【问题标题】:Laravel Livewire Star RatingLaravel Livewire 星级评定
【发布时间】:2021-05-31 23:01:34
【问题描述】:

我需要完成评分开始的功能。在此代码中,两个模型使用 Post 模型和 PostRate 模型。 Post Model 具有 id、title、description 三个属性 PostRate 有 id、post_id、rate。 我已经制作了 livewire 组件 Raterate

的资源
<?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


【解决方案1】:

假设您处理 Post 和 Rate 之间的关系,并且可能是多对多的,并确定您的函数是否会为同一用户接受多个投票。也许如果你能详细解释一下你的任务

【讨论】:

    猜你喜欢
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-26
    • 1970-01-01
    • 2022-06-29
    • 1970-01-01
    • 2022-09-27
    相关资源
    最近更新 更多