使用ajax保存Laravel评级数据

时间:2017-09-29 01:18:40

标签: php ajax laravel

我尝试用laravel 5.5中的ajax保存评级后的数据

这是我的表格:

 <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', 'FrontendController@foodStar')->name('foodStar');

我的软件包的默认代码提供了保存费率的静态方法,但我需要获取用户将选择的内容,包的示例代码在我的控制器代码中被注释。

感谢。

2 个答案:

答案 0 :(得分:1)

HTML:

<input type="hidden" name="id" value="{{ $foodId }}">

使用Javascript:

$('#addStar').change('.star', function(e) {
  $.ajaxSetup({
    headers: {
      'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
    }
  });
  $.ajax({
    type: 'POST',
    cache: false,
    dataType: 'JSON',
    url: $(this).attr('action'),
    data: $(this).serialize(),
    success: function(data) {
      console.log(data);
    }
  });
});

控制器:

namespace ...
...
use Illuminate\Http\Request;
...

public function foodStar (Request $request) {
    // $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 = $request->input('star');
    $food = Food::find($request->input('id'));
    $food->ratings()->save($rating);
    return response()->json(['rating' => $rating->rating]);
}

答案 1 :(得分:0)

解决方案非常简单。

data: $('#addStar').serialize(),更改为data: {rating: $('input:checked').val()},

对于服务器端

public function foodStar(Request $request){
        // $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 = $request->rating; //get new star count;
        $food->ratings()->save($rating);
        return response()->json(['rating' => $rating->rating]);
    }

您还应该更改ajax中的网址