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



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

产品评级.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.blad.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

只有身份验证用户才能进行评论和评分。我希望只有订购该产品的用户可以发表评论。

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

通过此操作,您可以只允许对购买该产品的用户进行评级。

请像下面这样更新您的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');
}

最新更新