Php 在Laravel中为文章添加书签

Php 在Laravel中为文章添加书签,php,laravel,Php,Laravel,我试图创建一个功能,让用户能够书签和文章,并删除他的书签以及文章。为一篇文章添加书签的功能很好,但是当我尝试从书签中删除该文章时,它不起作用,而是插入了相同的记录,但文章id为NULL 这是我的控制器: public function postBookmark() { $user_id = Auth::user()->id; $article_id = Input::get('id'); $bookmark = Bookmark::where('user_

我试图创建一个功能,让用户能够书签和文章,并删除他的书签以及文章。为一篇文章添加书签的功能很好,但是当我尝试从书签中删除该文章时,它不起作用,而是插入了相同的记录,但文章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() }}
{{Form::open(数组('action'=>)BookmarkController@postBookmark')) }}  
书签
{{Form::close()}}

对于此功能,我也有一个带有post方法的路由。如果有人能告诉我为什么它不起作用,我将不胜感激。

如果你真的愿意,将条件
如果(!empty($bookmark))
更改为
如果($bookmark->count()){
可能会起作用,但它会用
count()
对数据库进行另一次查询,这并不是一个好方法

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


首选的方法是使用雄辩的模型关系。例如,对于关系,您可以通过
Article::has('bookmark')
进行检查。

如果您确实愿意,将条件
If(!empty($bookmark))
更改为
If($bookmark->count()){
可能会起作用,但它会使用
count()进行另一次查询
对于DB来说,这并不是一个很好的方法

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


首选的方法是使用雄辩的模型关系。例如,对于关系,您可以通过
Article::has('bookmark')
进行检查。

您没有执行书签查询

代码示例中的
$boomark
变量是一个
illighted\Database\elount\Builder
对象,
空($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();

那么它应该可以正常工作。

您没有执行书签查询

代码示例中的
$boomark
变量是一个
illighted\Database\elount\Builder
对象,
空($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();

那么它应该可以正常工作。

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

    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() }}
{{Form::open(数组('action'=>)BookmarkController@postBookmark')) }}     
书签
{{Form::close()}}

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

    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() }}
{{Form::open(数组('action'=>)BookmarkController@postBookmark')) }}     
书签
{{Form::close()}}
注意:
Auth::user()->id
可以写成
Auth::id()
注意:
Auth::user()->id
可以写成
Auth::id()