Reputation: 626
I'm doing a laravel project and I want to load notification via a ajax call. I wrote a code to do it. This is my ajax call.
$(document).ready(function () {
$('#notificationBar').on('click', function () {
getNotifications();
});
function getNotifications(){
var getNotificationURL = '{{ route('getNotifications') }}';
var token = '{{ Session::token() }}';
$.ajax({
method: 'post',
url : getNotificationURL,
data : {
_token : token
},
success:function (data) {
if(data === null){
}else{
$('#notificationBar').find('#notificationDropDown').find('#tempId').append('<a style="background:#e0e0d1; margin:2px 2px;" href="'+data.username+'"><img src="'+data.url+'" class="w3-circle" style="height:40px;width:40px;margin:4px 4px;" alt="Avatar"> <b> '+data.name+'</b> '+data.msg+'</a>');
}
}
});
}
});
and this is my route
Route::post('getnotifications', [
'uses' => 'NotificationController@getNotifications',
'as' => 'getNotifications'
]);
the route will direct to this method in the notification controller
public function getNotifications(Request $request){
$noti = DB::table('notifications')->where('accepter', Auth::user()->id)->where('status', 1)->where('ajax_call',true)->where('important',true)->first();
if(count($noti)>0){
$new_noti = Notifications::find($noti->id);
$new_noti->ajax_call = false;
$new_noti->update();
$notification = DB::table('notifications')->leftjoin('users','users.id','notifications.current_user')->where('id', $noti->id)->first();
$pro_ulr = asset('/img/'.$notification->pic);
$arr = array(
"username" => $notification->username,
"url" => $pro_ulr,
"name" => $notification->name,
"msg" => $notification->msg
);
return response()->json($arr);
}else{
return null;
}
}
My problem is this function isn't working as I wish. Database table doesn't update and json seems to be empty. Is there something wrong with my code?
Upvotes: 2
Views: 134
Reputation: 496
I have updated your controller's action code, hope this will resolved your issue:
public function getNotifications(Request $request)
{
$notifications = Notifications::where('accepter', Auth::user()->id)->where('status', 1)->where('ajax_call', true)->where('important', true)->get();
if ( $notifications->count() === 0 ) {
return null;
}
$newNotification = $notifications->first();
$newNotification->ajax_call = false;
$newNotification->save();
$notification = DB::table('notifications')->leftjoin('users', 'users.id', 'notifications.current_user')->where('id', $noti->id)->first();
$pro_ulr = asset('/img/'.$notification->pic);
$arr = [
"username" => $notification->username,
"url" => $pro_ulr,
"name" => $notification->name,
"msg" => $notification->msg,
];
return response()->json($arr);
}
Your had an issue with, Table::first()
you need to call get()
instead of first()
. Also instead of $new_noti->update();
simply $new_noti->save();
will work.
Upvotes: 1
Reputation: 458
Your code has one spelling mistake.
I think that you know how to fix it. When you checking, you should enable Chrome Developer Tool and choose Network tab for verifying that that request was executed.
Upvotes: 0