Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery ajax没有';t更新数据库_Jquery_Ajax_Laravel - Fatal编程技术网

Jquery ajax没有';t更新数据库

Jquery ajax没有';t更新数据库,jquery,ajax,laravel,Jquery,Ajax,Laravel,我正在做一个laravel项目,我想通过ajax调用加载通知。我写了一个代码来做这件事。这是我的ajax调用 $(document).ready(function () { $('#notificationBar').on('click', function () { getNotifications(); }); function getNotifications(){ var getNotificationURL = '{{ ro

我正在做一个laravel项目,我想通过ajax调用加载通知。我写了一个代码来做这件事。这是我的ajax调用

$(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>');

                }
            }
        });
    }

});
路由将指向通知控制器中的此方法

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

}

我的问题是这个函数没有按我所希望的那样工作。数据库表未更新,json似乎为空。我的代码有问题吗?

您的代码有一个拼写错误


我想你知道怎么修理它。检查时,应启用Chrome Developer Tool,并选择Network选项卡以验证该请求是否已执行。

我已更新了控制器的操作代码,希望这能解决您的问题:

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

}

您的问题是,
Table::first()
您需要调用
get()
,而不是
first()
。也不是
$new_noti->update()只需
$new_noti->save()将起作用。

您应该在计数($noti)>0条件中登录,它会进入吗?抱歉。我不明白。您希望我做什么?在这个条件中:“if(count($noti)>0){”do a\Log::info('here'))要查看是否,在laravel日志中,它记录了它是否进入了该代码块。您的查询只会第一次运行。从下一次开始将为空。哦,上帝。谢谢。它对我有效;Dyeah我后来看到了它。但代码仍然不起作用。数据库未更新
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);

}