首页 > 解决方案 > 只有买家才能在 Livewire:Laravel 中对产品进行评分

问题描述

我在我的 Laravel 电子商务应用程序中使用 Livewire 评级系统。现在任何用户都可以对任何产品进行评分和评论。我希望只有买家可以评价和评论。

ProductRatings.php

class ProductRatings extends Component
{
    public $rating;
    public $comment;
    public $currentId;
    public $product;
    public $hideForm;

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

    ];

    public function render()
    {
        $comments = Rating::where('product_id', $this->product->id)->where('status', 1)->with('user')->get();
        if(auth()->user()) {
            $user = Eorder::where('user_id', auth()->user()->id);
        }
        
        return view('livewire.product-ratings', compact('comments', 'user'));
    }

    public function mount()
    {
        if (auth()->user()) {
            $rating = Rating::where('user_id', auth()->user()->id)->where('product_id', $this->product->id)->first();
            if (!empty($rating)) {
                $this->rating  = $rating->rating;
                $this->comment = $rating->comment;
                $this->currentId = $rating->id;
            }
        }
        return view('livewire.product-ratings');
    }

    public function delete($id)
    {
        $rating = Rating::where('id', $id)->first();
        if ($rating && ($rating->user_id == auth()->user()->id)) {
            $rating->delete();
        }
        if ($this->currentId) {
            $this->currentId = '';
            $this->rating  = '';
            $this->comment = '';
        }
    }

    public function rate()
    {
        $rating = Rating::where('user_id', auth()->user()->id)->where('product_id', $this->product->id)->first();
        $this->validate();
        if (!empty($rating)) {
            $rating->user_id = auth()->user()->id;
            $rating->product_id = $this->product->id;
            $rating->rating = $this->rating;
            $rating->comment = $this->comment;
            $rating->status = 1;
            try {
                $rating->update();
            } catch (\Throwable $th) {
                throw $th;
            }
            session()->flash('message', 'Success!');
        } else {
            $rating = new Rating;
            $rating->user_id = auth()->user()->id;
            $rating->product_id = $this->product->id;
            $rating->rating = $this->rating;
            $rating->comment = $this->comment;
            $rating->status = 1;
            try {
                $rating->save();
            } catch (\Throwable $th) {
                throw $th;
            }
            $this->hideForm = true;
        }
    }
}

产品评级.blade.php

@auth
                            @if($hideForm != true)
                                <form wire:submit.prevent="rate()" class="form-horizontal" id="form-review">
                                    <div id="review"></div>
                                    <div class="col-sm-12 form-group required">
                                        <div class="flex space-x-1 rating">
                                            <label class="control-label">Rating: </label> &nbsp;&nbsp;&nbsp;
                                            <label for="star1">
                                                <input hidden wire:model="rating" type="radio" id="star1" name="rating" value="1" />
                                                <svg class="cursor-pointer block w-8 h-8 @if($rating>= 1 ) text-orange-400 @else text-grey @endif " fill=" currentColor" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
                                                    <path d="M10 15l-5.878 3.09 1.123-6.545L.489 6.91l6.572-.955L10 0l2.939 5.955 6.572.955-4.756 4.635 1.123 6.545z" />
                                                </svg>
                                            </label>
                                        </div>
                                        <div class="my-5 ">
                                            @error('comment')
                                            <p class="mt-1 text-red-500">{{ $message }}</p>
                                            @enderror
                                            <label class="control-label" for="input-review">Your Review</label>
                                            <textarea wire:model.lazy="comment" name="description" class="form-control" rows="5" placeholder="Comment.."></textarea>
                                        </div>
                                    </div>
                                    <div class="block">
                                        <button type="submit" class="btn btn-primary">Rate</button>
                                        @auth
                                        @if($currentId)
                                        <button type="submit" class="btn btn-danger" wire:click.prevent="delete({{ $currentId }})" class="text-sm cursor-pointer">Delete</button>
                                        @endif
                                        @endauth
                                    </div>
                                </form>
                            @endif
                        @else
                            <div class="review-button">
                                <div class="mb-8 text-center text-gray-600">
                                    You need to login in order to be able to rate the product!
                                </div>
                                <a href="/register" class="px-5 py-2 mx-auto font-medium text-center text-gray-600 bg-white border rounded-lg shadow-sm focus:outline-none hover:bg-gray-100" style="width:45%;padding:10px;display:block;float:left;margin-left:20px;">Register</a>
                                <a href="/login" class="px-5 py-2 mx-auto font-medium text-center text-gray-600 bg-white border rounded-lg shadow-sm focus:outline-none hover:bg-gray-100" style="width:45%;padding:10px;display:block;float:left;margin-left:20px;">Login</a>
                            </div>
                        @endauth

只有 auth 用户可以评论和评分。我只希望订购产品的用户可以发表评论。

标签: javascriptphpcsslaravellaravel-livewire

解决方案


我还没用Livewire。但我可以向您展示它如何在laravel 8. 检查用户购买历史。使用变量来检查用户是否购买了产品。

$is_purchased = 0;

检查用户是否购买了产品。如果购买比改变价值$is_purchased

$purchased = Orders::where('user_id', auth()->user()->id)->where('product_id', $this->product->id)->first();
if($purchased){
    $is_purchased = 1;
} 

发送$is_purchased到您的视图并检查它的价值

if($is_purchased == 1){
 // do rating stuff
}

这样您就可以只对购买该产品的人进行评分。


推荐阅读