救我尝试后等级的数据与阿贾克斯保存laravel 5.5Laravel评级数据与阿贾克斯
这里是我的形式:
<form class="foodstars" action="{{route('foodStar')}}" id="addStar" method="POST">
{{ csrf_field() }}
<input class="star star-5" value="5" id="star-5" type="radio" name="star"/>
<label class="star star-5" for="star-5"></label>
<input class="star star-4" value="4" id="star-4" type="radio" name="star"/>
<label class="star star-4" for="star-4"></label>
<input class="star star-3" value="3" id="star-3" type="radio" name="star"/>
<label class="star star-3" for="star-3"></label>
<input class="star star-2" value="2" id="star-2" type="radio" name="star"/>
<label class="star star-2" for="star-2"></label>
<input class="star star-1" value="1" id="star-1" type="radio" name="star"/>
<label class="star star-1" for="star-1"></label>
</form>
这里是我的Ajax:
<script>
$('#addStar').onclick('.foodstars',function(e) {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
}
});
e.preventDefault();
$.ajax({
type: 'POST',
cache: false,
dataType: 'JSON',
url: '/',
data: $('#addStar').serialize(),
success: function(data) {
console.log(data);
},
});
return false;
});
</script>
这是我的控制器:
public function foodStar(){
// $rating = new Rating;
// $rating->rating = 4;
// $rating->user_id = \Auth::id();
// $food->ratings()->save($rating);
$rating = new Rating;
$rating->user_id = \Auth::id();
$rating->rating = //get new star count;
$food->ratings()->save($rating);
return response()->json(['rating' => $rating->rating]);
}
这里是我的路线:
Route::post('/rating', '[email protected]')->name('foodStar');
我的包的默认代码提供的静态方法来保存率,但我需要得到将选择什么样的用户,包的示例代码在我的控制器代码注释。
谢谢。
不保存数据。 – mafortis
@mafortis如果你做了一个'dd($ request-> input('star'))'它是否显示评分值?在分配请求值后,“dd($ rating)”的输出是什么? –
如果我使用'dd($ request-> input('star'))'获取加载页面的错误,如果我使用'dd($ rating)'或'dd($ rating-> rating);'什么都不会发生。 – mafortis