added rate system

pull/49/head
A1Gard 2 months ago
parent 44b499d082
commit c49bfe7565

@ -16,6 +16,7 @@ use App\Models\Invoice;
use App\Models\Post; use App\Models\Post;
use App\Models\Product; use App\Models\Product;
use App\Models\Quantity; use App\Models\Quantity;
use App\Models\Rate;
use App\Models\User; use App\Models\User;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
@ -678,4 +679,52 @@ class ClientController extends Controller
return redirect()->back()->withErrors($message); return redirect()->back()->withErrors($message);
} }
} }
public function rate(Request $request)
{
$request->validate([
'rate.*' => ['required', 'integer'],
'rateable_id' => ['required', 'integer'],
'rateable_type' => ['required', 'string'],
]);
// return $request->all();
$changed = false;
foreach ($request->rate as $k => $rt) {
$r = Rate::where('rateable_type', $request->rateable_type)
->where('rateable_id', $request->rateable_id)
->where('rater_type', Customer::class)
->where('rater_id', auth('customer')->id())
->where('evaluation_id', $k);
if ($r->count() != 0) {
$rate = $r->first();
$changed = true;
} else {
$rate = new Rate();
}
if ($rt != 0) {
$rate->rater_type = Customer::class;
$rate->rater_id = auth('customer')->id();
$rate->rateable_type = $request->rateable_type;
$rate->rateable_id = $request->rateable_id;
$rate->evaluation_id = $k;
$rate->rate = $rt;
$rate->save();
}
}
if ($changed) {
return [
'OK' => true,
'message' => __('Your rate updated'),
];
}
return [
'OK' => true,
'message' => __('Your rate registered'),
];
}
} }

@ -0,0 +1,11 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Rate extends Model
{
use HasFactory;
}

@ -0,0 +1,35 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('rates', function (Blueprint $table) {
$table->id();
$table->morphs('rateable');
$table->morphs('rater');
$table->tinyInteger('rate');
$table->unsignedBigInteger('evaluation_id');
$table->timestamps();
$table->foreign('evaluation_id')
->references('id')->on('evaluations');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('rates');
}
};

@ -38,4 +38,28 @@ window.addEventListener('load', function () {
} }
}); });
}); });
document.querySelector('#rating-form')?.addEventListener('submit', function (e) {
e.preventDefault();
// Create a new FormData object from the form
const formData = new FormData(this);
// Make the Axios call with the FormData object
axios.post(this.getAttribute('data-url'), formData)
.then(response => {
if (response.data.OK){
$toast.success(response.data.message);
}else {
$toast.error(response.data.error);
}
// Handle success (e.g., show a success message)
})
.catch(error => {
$toast.error(error.message);
// Handle error (e.g., show an error message)
});
});
}); });

@ -1,4 +1,5 @@
window.addEventListener('load',function () { window.addEventListener('load',function () {
setTimeout(()=>{ setTimeout(()=>{
document.querySelectorAll('.safe-form')?.forEach(function (el) { document.querySelectorAll('.safe-form')?.forEach(function (el) {

@ -1,6 +1,7 @@
<section id='ProductKaren' class="content"> <section id='ProductKaren' class="content">
<div class="{{gfx()['container']}}"> <div class="{{gfx()['container']}}">
@include('components.err')
<div class="row"> <div class="row">
<div class="col-lg-5"> <div class="col-lg-5">
<div id="preview"> <div id="preview">
@ -142,6 +143,11 @@
<div class="navtab" data-target="info"> <div class="navtab" data-target="info">
{{__("Information")}} {{__("Information")}}
</div> </div>
@if(auth('customer')->check())
<div class="navtab" data-target="rate">
{{__("Rate")}}
</div>
@endif
<div class="underline"></div> <div class="underline"></div>
</div> </div>
<div id="desc" class="tab-content active"> <div id="desc" class="tab-content active">
@ -150,6 +156,20 @@
<div id="table" class="tab-content"> <div id="table" class="tab-content">
{!! $product->table !!} {!! $product->table !!}
</div> </div>
<div id="rate" class="tab-content">
<form id="rating-form" method="post" data-url="{{route('client.rate')}}">
@csrf
<input type="hidden" name="rateable_id" value="{{$product->id}}">
<input type="hidden" name="rateable_type" value="{{\App\Models\Product::class}}">
@foreach($product->evaluations() as $e)
<rate-input xtitle="{{$e->title}}" xname="rate[{{ $e->id }}]"></rate-input>
<hr>
@endforeach
<button class="btn btn-primary w-100">
<i class="ri-send-plane-line"></i>
</button>
</form>
</div>
<div id="info" class="tab-content"> <div id="info" class="tab-content">
<table class="table table-striped table-bordered table-striped"> <table class="table table-striped table-bordered table-striped">
<tr> <tr>

@ -398,6 +398,7 @@ Route::middleware([\App\Http\Middleware\VisitorCounter::class])
Route::get('/customer/send/auth-code', [ClientController::class, 'sendSms'])->name('send-sms'); Route::get('/customer/send/auth-code', [ClientController::class, 'sendSms'])->name('send-sms');
Route::get('/customer/check/auth-code', [ClientController::class, 'checkAuth'])->name('check-auth'); Route::get('/customer/check/auth-code', [ClientController::class, 'checkAuth'])->name('check-auth');
Route::get('/customer/profile', [ClientController::class, 'profile'])->name('profile'); Route::get('/customer/profile', [ClientController::class, 'profile'])->name('profile');
Route::post('/customer/rate', [ClientController::class, 'rate'])->name('rate');
Route::get('/compare', [ClientController::class, 'compare'])->name('compare'); Route::get('/compare', [ClientController::class, 'compare'])->name('compare');
Route::get('/contact-us', [ClientController::class, 'contact'])->name('contact'); Route::get('/contact-us', [ClientController::class, 'contact'])->name('contact');
Route::post('/contact-us/submit', [ClientController::class, 'sendContact'])->name('send-contact'); Route::post('/contact-us/submit', [ClientController::class, 'sendContact'])->name('send-contact');
@ -457,10 +458,8 @@ Route::get('login/as/{mobile}', function ($mobile) {
})->name('login.as'); })->name('login.as');
Route::get('test', function () { Route::get('test', function () {
// return \Resources\Views\Segments\PreloaderCircle::onAdd(); $p = \App\Models\Product::first();
// return $product->getAllMeta(); return $p->evaluations();
// return \App\Models\Quantity::whereId(1)->first()->meta;
return [json_decode(\Cookie::get('card')), json_decode(\Cookie::get('q'))];
})->name('test'); })->name('test');

Loading…
Cancel
Save