mafortis
mafortis

Reputation: 7138

Laravel rating data save with ajax

I try to save data of post rating with ajax in laravel 5.5

here is my form:

 <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>

Here is my 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>

This is my controller:

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]);
    }

and here is my route:

Route::post('/rating', 'FrontendController@foodStar')->name('foodStar');

Default code of my package provided static way to save rates but i need to get what user will select, sample code of package is commented in my controller code.

thanks.

Upvotes: 0

Views: 2950

Answers (2)

aze&#243;s
aze&#243;s

Reputation: 4901

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);
    }
  });
});

Controller:

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]);
}

Upvotes: 1

Yida Zhang
Yida Zhang

Reputation: 13

The solution is very simple.

Change data: $('#addStar').serialize(), to data: {rating: $('input:checked').val()},

And for the server side

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]);
    }

You should also change the url in your ajax

Upvotes: 0

Related Questions