favorites - 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];
}
解决方案
我的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) {
}
});
});
});
推荐阅读
- python - 将鼠标光标移动到从文本文件导入的坐标
- reactjs - ReactJs MaterialUi onRowUpdate 字段验证
- redirect - IIS 重定向规则将一个 url 重定向到另一个
- c - 需要帮助理解这个用函数模拟 strcpy() 的 C 程序
- xml - 逻辑应用程序中的 XPath 根据其类型提取值
- android - 删除 PreferenceCategory 标题中的填充和边距
- javascript - “状态”是 React 中的保留字吗?
- java - 将日期格式从 Java 转换为 SQL
- r - (R) 累积计算序号中的间隙
- docker - 无法访问使用 Nginx 服务 Dockerized VueJS 应用程序并导致 SSL 错误