Laravel-买方如何对产品进行评论? [英] Laravel - how to make a review by buyer for the product?

查看:53
本文介绍了Laravel-买方如何对产品进行评论?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问兄弟,我试图使购买商品的用户对产品进行评论,但尝试却没有成功,结果是这样的:

I want to ask Bro, how to make a product review by the user who has bought the goods, I've tried to make, but not successful, the result is like this:

首先,如果用户(买方)购买了商品并成功进行了审核,但是该审核表仍然存在...我该如何删除已进行审核的用户(买方)的表单货吗?

first, if the user (buyer) buy the goods and do the review is successful, but the form for the review is still there ... how do I remove the form for the user (buyer) who has done a review for the goods?

可以对产品进行评论,而无需对产品进行评论#View Image

尽管查看了商品#View Image

其次,它已经被其他用户购买并审查过,该用户(具有不同帐户的买方)进行了购买并成功,但是如果对同一项目进行审查,则不会出现表格,但是已经作出了购买..

secondly, it is already purchased and reviewed by other users, the user (buyer with different account) make a purchase and succeed, but if make a review of the same item, can not the form does not appear, but already made a purchase ..

即使购买了#View Image,用户也没有任何形式

基本上是这样的如果尚未对货物进行审查,则可以进行审查,并且即使已经进行了审查,表格仍然存在.但是如果您已有评论,则其他用户(买方)将无法评论该项目(该表格不会显示).好吧,这是Bro?在条件_(如果)_中处于哪里的问题?还是其他?

essentially like this if the goods have not been reviewed, can do the review and the form is still there even though it has done a review. but if you already have a review, other users (buyers) can not review the item (the form does not appear). well that's the problem where is Bro ?, in conditioning _ (if) _? or anything else?

viewproduct.blade.php

<div role="tabpanel" class="tab-pane fade" id="profile">
<h2>Reviews</h2>
@foreach($reviews as $data)
<div class="panel panel-default">
    <div class="panel-body">
        <h4>{{ $data->user['name'] }}</h4>
        <p>{{ $data->ulasan }}</p>
        <h3>{{ $data->rating }}</h3>
    </div>
</div>
@endforeach
@if(Auth::user() && Auth::user()->id == $show->order['user_id'])
@if($errors->any())
<div class="alert alert-warning">
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
//form for review (I cut the code)
@endif
</div>

StoreController.php

public function ViewProduct($id)
{
    $show = Products::findOrFail($id);
    $related = Products::where('kategori_id', $show->kategori_id)
    ->orderByRaw('RAND()')
    ->take(10)
    ->get();
    $reviews = Reviews::where('product_id', $show->id)->get();
    return view('shop.viewproduct', compact('show','related','order','reviews'));
}

public function StoreReviewProduct(Request $request)
{
    $this->validate($request, [
        'rating' => 'required',
        'description' => 'required|min:10',
    ]);
    $addreview = new Reviews([
        'product_id' => $request['product_id'],
        'user_id' => Auth::user()->id,
        'rating' => $request['rating'],
        'description' => $request['description']
    ]);
    $addreview->save();
    Session::flash('success','thanks for adding review!');
    return redirect()->back();
}

产品(模型).php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Products extends Model
{
    protected $fillable = ['kategori_id','nama_product','deskripsi','harga','pict','stok','review','id_kios'];

    public function kios()
    {
        return $this->belongsTo(Kios::class,'id_kios');
    }

    public function order()
    {
        return $this->belongsTo(Orders::class,'id','product_id');
    }
}

非常感谢您的回答:)

推荐答案

似乎您已经设置了自己的评论模型.

It looks like you are already set up with your Reviews model.

在产品"模型中,您可以考虑创建一个函数,该函数返回一个布尔值,用于判断当前用户是否提交了评论.这将允许您在刀片文件中设置@if条件.

In your Products model, you could consider creating a function that returns a boolean for whether or not a review has been submitted by the current user. This will allow you to set an @if condition in your blade file.

public function reviews(){
    return $this->belongsToMany(Reviews::class);
}

public function currentUserHasSubmittedReview(){
    $countOfReviews = $this->reviews()
        ->where('user_id', Auth::user()->id)
        ->where('product_id', $this->id)
        ->get();

    return ($countOfReviews > 1 ? true : false); 
}

然后用刀片服务器

@if($product->currentUserHasSubmittedReview() == false)

 <!-- REVIEW CODE -->

@endif

如果对此有疑问,请告诉我.

Please let me know if there are questions about how this would work.

这篇关于Laravel-买方如何对产品进行评论?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆