在Laravel中为一篇文章添加书签

时间:2014-07-31 17:02:16

标签: php laravel

我正在尝试为用户创建一个功能,以便能够添加书签和文章,并从书签中删除该文章。为文章添加书签的功能很好,但是当我尝试从书签中删除文章时,它不起作用,而是插入相同的记录,但article_id为NULL。

这是我的控制器:

public function postBookmark() {
    $user_id    = Auth::user()->id;
    $article_id = Input::get('id');
    $bookmark   = Bookmark::where('user_id', '=', $user_id)->where('article_id', '=', $article_id);
    $article    = Article::where('id', '=', $article_id);
    $article    = $article->first();

    // I want to check if the article has been already bookmarked by the same user
    // but this if statement always returns true
    if(!empty($bookmark)) { 
        $bookmark = Bookmark::create(array(
             'user_id' => $user_id,
             'article_id' => $article_id,
         ));

        if($bookmark) {
            return View::make('article.view')
               ->with('article', $article)
               ->with('bookmarked', true);  
        }       
    } else {
        // Does not work
        $bookmark->delete();

        return View::make('article.view')
               ->with('article', $article)
               ->with('bookmarked', false);
    }

    return Redirect::route('article-all')
           ->with('global', 'We were unable to bookmark the article. Please, try again later.');
}

这是我观点的一部分:

{{ Form::open(array('action' => 'BookmarkController@postBookmark')) }}  
    <input 
         type="checkbox" 
         name="id" 
         onClick="this.form.submit()"
         value="{{ $article->id }}" 
         id="bookmark" 
         {{ $bookmarked ? 'checked' : '' }}
    />
    <label for="bookmark">Bookmark</label>
{{ Form::close() }}

我也有一个带有此功能的post方法的路由。如果有人能够知道为什么它不起作用,我将非常感激。

3 个答案:

答案 0 :(得分:2)

您没有执行书签查询。

代码示例中的$boomark变量是Illuminate\Database\Eloquent\Builder对象,empty($boomark)将始终返回false,因为存储了一个对象。

要执行查询,您可以使用get()。在您的情况下,您只需要一个结果,然后使用first()检索第一个找到的书签对象。

变化:

$bookmark = Bookmark::where('user_id', '=', $user_id)->where('article_id', '=', $article_id);

对此:

$bookmark = Bookmark::where('user_id', '=', $user_id)->where('article_id', '=', $article_id)->first();

然后它应该可以正常工作。

答案 1 :(得分:1)

感谢您的帮助。我最终使用了你的两个解决方案。

    public function postBookmark() {
        $user_id = Auth::id();
        $article_id = Input::get('id');
        $bookmark = User::find($user_id)->bookmarks()->where('article_id', '=', $article_id)->first();

        if(empty($bookmark)) {
             $bookmark = Bookmark::create(array(
                 'user_id' => $user_id,
                 'article_id' => $article_id,
             ));

             if($bookmark) {
                 return     Redirect::route('article-get', array('article_id' => $article_id));
             }
        } else {
             $bookmark->delete();
             return     Redirect::route('article-get', array('article_id' => $article_id));
        }   
        return  Redirect::route('article-all')
            ->with('global', 'We were unable to bookmark the article. Please, try again later.');
    }

尽管如此,我真正需要解决的是我的观点。由于某种原因,我输入的ID没有正确提交,所以我最终也为它创建了一个隐藏字段。

     {{ Form::open(array('action' => 'BookmarkController@postBookmark')) }}     
         <input 
             type="checkbox" 
            name="id" 
            onClick="this.form.submit()"
            value="{{ $article->id }}" 
            id="bookmark" 
            {{ $bookmarked ? 'checked' : '' }}
         />

         <input 
              type="hidden"
             name="id" 
             value="{{ $article->id }}"  
         />
        <label for="bookmark">Bookmark</label>
    {{ Form::close() }}

答案 2 :(得分:0)

如果您真的想要,将条件if(!empty($bookmark))更改为if ($bookmark->count()) {可能会有所帮助,但它会使用COUNT()向DB执行另一个查询,这不是一个好方法这样做。

问题在于if(!empty($bookmark)),因为$bookmark有一个QueryBuilder实例,它永远不会为空。

首选方式是使用Eloquent模型关系。通过关系,您可以通过Article::has('bookmark')进行检查。