How to use Laravel Notify 2 Package with ajax?
Using Laravel Notify 2, I want to show a notification after delete a wishlist item via ajax. But, how? Here is my code:
Route:
Route::post('/remove/wishlist/', 'Fontend\Wishlist\WishlistController@destroy')->name('delete.wishlist');
Controller Method:
public function destroy(Request $request) { $validatedData = $request->validate([ 'product_id' => 'required', ]); $userid = Auth::id(); $id = $request->product_id; $delete = Wishlist::where('user_id',$userid)->where('product_id',$id)->delete(); if ($delete) {
// notify()->success('Successfully removed from wishlist','Wishlist');
// return Redirect()->back();
$wish_count = Auth::user()->wishlist->count();
return json_encode(['status' =>'success', 'totalItems' => $wish_count]);
}
else {
notify()->warning('Something went to wrong','Wishlist');
return Redirect()->back();
}
}
Java Script:
function removeWishlist(product_id){ // add csrf token code start $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } }); $.post( "/remove/wishlist/", { product_id: product_id }) .done(function( data ) { data = JSON.parse(data); // after request success if(data.status == 'success'){ $("#wishlistItemsDT,#wishlistItemsMB").html(data.totalItems); $('.addToWishlistclass'+product_id).show(); $('.removeWishlistclass'+product_id).hide(); } }); }
Please or to participate in this conversation.