只有买家可以在 Livewire:Laravel 中对产品进行评分

Posted

技术标签:

【中文标题】只有买家可以在 Livewire:Laravel 中对产品进行评分【英文标题】:Only a buyer can rate the product in Livewire:Laravel 【发布时间】:2021-11-27 06:16:48 【问题描述】:

我在我的 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;
        
    

product-ratings.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

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

【问题讨论】:

在我阅读所有内容之前:到目前为止,您尝试过什么吗?我假设购买最终在数据库中,所以当用户 X 访问产品 Y 的页面时,你不应该能够弄清楚他们是否购买了产品 Y?另外,您的问题听起来您甚至可能不知道这种方法? 是的,我是第一次这样做,所以我不知道如何编写代码以及应该在哪里编写。 【参考方案1】:

我还没有使用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

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

【讨论】:

【参考方案2】:

请更新您的 mount() 函数,如下所示。

public function mount()
  
    if (auth()->user()) 
      $rating = Rating::where('user_id', auth()->user()->id)>where('product_id', $this->product->id)->first();


      $purchased = Orders::where('user_id', auth()->user()->id)->where('product_id', $this->product->id)->first();
      if ($purchased) 
        $this->hideForm = true;
       else 
        $this->hideForm = false;
      
      if (!empty($rating)) 
        $this->rating  = $rating->rating;
        $this->comment = $rating->comment;
        $this->currentId = $rating->id;
      
    
    return view('livewire.product-ratings');
  

【讨论】:

以上是关于只有买家可以在 Livewire:Laravel 中对产品进行评分的主要内容,如果未能解决你的问题,请参考以下文章

将参数(当前选择的 $this->user->id 而不是 auth()->user()->id)从视图传递到 livewire + laravel 中的 createShow

卖家使用iOS设备扫描买家的信用卡完成付款[关闭]

检查支付宝异步通知是否正确

淘宝 - 淘宝买家信誉怎么查询?

iPhone X萎靡让OLED屏市场供大于求 三星急寻买家...

秒杀确认排期超时怎么办