[英]How to fetch user ratings with stars in laravel
以下代碼可以設置星號並將數字傳遞給數據庫並進行設置。 但是,當我刷新頁面時,星星消失了,因此不清楚用戶是否進行了評論。
我希望能夠使用指示評分的星標來獲取用戶評分。
我使用的是所謂的包裝應課差餉租和rateYo ,讓您在星宿。
(下面您可以將一顆星星設置好,然后提交將其傳遞到數據庫中)
當您轉介時,您會得到這個
所以我試圖獲取星星的數量。
這是我到目前為止所擁有的
Main.js
$(document).ready(function(){
'use strict'
$('#rateYo').rateYo({
starWidth: "20px",
});
$('#rateYo').click(function(){
var rating = $('#rateYo').rateYo("rating");
console.log(rating);
$('#val').val(rating);
});
// an ajax function goes here but not sure how to approach this and within the
// function i do know that rating needs to be set to the user submitted rating
//so that it can fetch the amount of starts
$('#rateYo').rateYo({
rating:1-5
});
});
show.blade.php
<form action="{{ route('rate', $book->id) }}" method="POST">
{!! csrf_field() !!}
<div id="rateYo"></div>
<input name="val" value='' type="hidden" id="val" >
<button type="submit" class="btn btn-primary mt-2">submit</button>
</form>
控制者
// this is where im stuck, this is where im attempting to fetch the stars from
//a user, but im not sure what im doing, i don't how i would pass it in
public function rating($rating)
{
$book = Book::getRatingAttribute($rating)->userSumRating() ;
return json_encode($book);
}
書(型號)
public function getRatingAttribute($rating)
{
return Rateable::where('rating', $rating )->where('user_id',Auth()->user()->id)->first();
}
路線
Route::get('rate/{rating}', 'BookController@rating');
關於如何解決這個問題或如何解決這個問題的任何建議?
首先以這種方式在HTML中打印您的評分。
<div id="rateYo" data-rating="{{ $book->rating }}"></div>
然后更改javascript以從data屬性設置評分:
var rating = $('#rateYo').data("rating")
$('#rateYo').rateYo({
starWidth: "20px",
rating: rating
});
我認為它將起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.