我想用Laravel制作一个喜欢的应用程序。我已经做了一部分。但是我喜欢的邮件不会保存到数据库。对不起我的英语不好
文件名:Denetleyici 我的代码:
public function postLikePost(Request $request)
{
$post_id = $request['postId'];
$is_like = $request['isLike'] == 'true';
$update = false;
$post = Post::find($post_id);
if(!$post) {
return null;
}
$user = Auth::user();
$like = $user->likes()->where('post_id', $post_id)->first();
if($like){
$already_like = $like->like;
$update = true;
if($already_like == $is_like){
$like->delete();
return null;
}
}else{
$like = new Like();
}
$like->like = $is_like;
$like->user_id = $user->id;
$like->post_id = $post->id;
if($update){
$like->update();
}else{
$like->save();
}
return null;
}
File.Name:App.js 我的代码:
$('.like').on('click', function (event) {
event.preventDefault();
postId = event.target.parentNode.parentNode.dataset['postid'];
var isLike = event.target.previousElementSibling == null;
$.ajax({
method: 'POST',
url: urlLike,
data: {isLike: isLike, postId: postId, _token: token}
})
.done(function ( ) {
//Change The Page
})
});