首页 > 解决方案 > send id product to Favorite list in database

问题描述

hello want to make Favorite list of product .but get 401 error please help me to make a Favorite list

my controller is:

 public function selectFavorite(Request $request)
{
    if(Auth::check()){
        if(isset($request->products_id)){
            $f_exit=Like::findOrFail('products_id',$request->products_id)->count();
            if($f_exit == 0){
                $f=Like::create($request->all());
                $f->user_id=Auth::user()->id;
                $f->save(); return ['status'=>200];
            }
            return ['status'=>301];
        }
        return ['status'=>401];
    }
    return ['status'=>305];
}

标签: favorites

解决方案


我的ajax代码

$(document).ready(function () {
    $('body').on('click', '.selectFavorite', function () {
        console.log('selectFavorite');
        var id = $(this).attr('id');
        var products_id = id.split('/')[1];
        var url = "UsersProfile/selectFavorite";
        $.ajax({
            type: "POST",
            url: url,
            headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
            data: {
                products_id: products_id,
            },
            success: function (result) {
                if (result['status'] == 200) {
                    //success
                    swal(" ", "Successfully added to favorites.", "success");
                }
                if (result['status'] == 301) {
                    //product and type was Favorite.
                    swal(" ", "This product has been added to favorites.", "warning");
                }
                if (result['status'] == 401) {
                    //isset is not id
                    swal(" ", "Error sending information", "warning");
                }
                if (result['status'] == 305) {
                    //user logout
                    swal(" ", "Please log in.", "warning");
                }
            },
            error: function (result) {
            }
        });
    });
});

推荐阅读