你如何传递角度的帖子ID?

时间:2017-11-27 07:11:42

标签: javascript angularjs laravel

如果使用laravel,如何以角度传递post id?

这就是我现在所拥有的,我试着引用这个

https://docs.angularjs.org/api/ng/service/ $ HTTP

但我真的不理解。

Main.js

$scope.like = function() {
    var post = {
        // this doesn't work and i dont know how to pull in the post id.
        id: "<% $post->id %>"
    };
    $http.post('post/like/'+ post).success(function(result) {
        checkLike();
    });
};
function checkLike(){
    var post = {
        id: "<% $post->id %>"
    };
    $http.get('post/'+ post '/islikedbyme').success(function(result) {
        if (result == 'true') {
            $scope.like_btn_text = "Delete Like";
        } else {
            $scope.like_btn_text = "Like";
        }
    });
};

路线

Route::get('post/{id}/islikedbyme', 'PostController@isLikedByMe');
Route::post('post/like', 'PostController@like');

控制器

public function isLikedByMe($id)
{
    $post = Post::findOrFail($id)->first();
    if (Like::whereUserId(Auth::id())->wherePostId($post->id)->exists()){
        return 'true';
    }
    return 'false';
}

public function like(Post $post)
{
    $existing_like = Like::withTrashed()->wherePostId($post->id)->whereUserId(Auth::id())->first();

    if (is_null($existing_like)) {
        Like::create([
            'post_id' => $post->id,
            'user_id' => Auth::id()
        ]);
    } else {
        if (is_null($existing_like->deleted_at)) {
            $existing_like->delete();
        } else {
            $existing_like->restore();
        }
    }
}

0 个答案:

没有答案
相关问题