Support the ongoing development of Laravel.io →
Database Eloquent Blade

I want to ask sir, I am beginner in laravel, I am making an online shop project with laravel but there is a problem here....

my problem :

first, the user (buyer) make the payment and successful, want to do a review (no record has been filled in tables reviews in the same product_id) successful .. but the review form is still there, how to remove it for users who have done a review? can do a review, when the product has not been reviewed can do a review, when the product has not been reviewed

when the user has done a review, the form is still there when the user has done a review, the form is still there

secondly, the user (another buyer with a different account) makes a purchase and payment (successful), wants to do a review .. can not because there is a record that has been filled for the same product, how to solve this? can not do a review for a product that already exists a review whereas a different user can not do a review for a product that already exists a review whereas a different user

Essentially, if the record review with the same empty product_id, can do a review but the form for the review is still available, but already do a review for the same goods, if the same product_id already, there can not do a review.

thank you :) 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();
}

Products(Model).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');
    }
}

thanks a lot for the answer :)

Last updated 3 years ago.
0

Hi Tova,

I would edit viewproduct.blade.php. In the for loop where you write the reviews check whether one of the review belongs to the current user. Before drawing the review just check whether that variable is true.

<h2>Reviews</h2>
<?php $reviewed = false; ?>
@foreach($reviews as $data)
@if($data->user['id'] == Auth::user()->id)
<?php $reviewed = true; ?>
@endif
...
@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
if(!$reviewed)
//form for review (I cut the code)
@endif
@endif

in the future you will probably not display all reviews, but page them. In that case you should find the value of $reviewed with a query to the database. Either in the controller and pass it to the view or directly in the view.

Last updated 7 years ago.
0

Sign in to participate in this thread!

PHPverse

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.