【问题标题】:laravel eloquent : Increments visits column by one for every visitslaravel 雄辩:每次访问将访问列增加一
【发布时间】:2015-06-23 07:16:27
【问题描述】:

我的站点中有我的帖子的模型。我的帖子模型有一个存储访问的列。当用户访问我的帖子时,我的帖子应该增加 1。 我完全知道如何做到这一点,但我的问题是当我在模型中将其增加 1 时,它会增加 2 !!!!!

我在控制器中编写了这段代码:

$new_post_inst = Post::where('id','=',$id)->first();
$new_post_inst->increment('visit');

这是我的控制器:

    public function get_id($id) {

    // cat

    $cat = Category::join('catrelations', 'catrelations.idcat', '=', 'categories.id')->orderBy('categories.id', 'ASC')->get();

    // menu

    $nav = Nav::orderBy('index', 'DESC')->get();

    $post = Post::find($id);

    $setting = Settings::find(1);

    $comments = Comment::where('post_id', '=', $id)->orderBy('id', 'asc')->get();

    $get_reply = array();

    $get_reply_id = array();

    //$counter = 0;

    //var_dump($comments);

    foreach ($comments as $comment) {

        $reply = Reply::where('parentId', '=', $comment->id)->get();

        if (!$reply->isEmpty()) {

            //$arr_reply[$comment->id] = $reply;

            //echo $comment->id.' has reply!!!!!<br>';

            //$reply_parent_id = $comment->id;

            $counter = 0;

            foreach ($reply as $replying) {

                $get_reply[$comment->id][$counter] = Comment::where('id', '=', $replying->comment_id)->get();

                //$comment_reply_id = $replying->comment_id;

                //$reply_arr = array();

                //$reply_arr[$comment->id] =  $comment_reply_id;

                //echo 'The reply is: '.$comment_reply_id.'<br>';

                foreach ($get_reply[$comment->id][$counter] as $key => $value) {

                    //($value->text);

                    $get_reply_id[] = $value->id;
                }

                $counter++;
            }

            //$counter++;


        }
    }

    $post_owner_info = User::select('id', 'first_name', 'last_name', 'image', 'desc')->find($post->user_id);

    $arr = array();

    $arr['comments'] = $comments;

    $arr['post'] = $post;

    //$arr['reply'] = $reply;

    //var_dump($get_reply);

    //var_dump($get_reply_id);
    $new_post_inst = Post::where('id','=',$id)->first();
    $new_post_inst->increment('visit');

    return View::make('blogsingle', compact('arr'))->with('setting', $setting)->with('post', $post)->with('nav', $nav)->with('get_reply', $get_reply)->with('get_reply_id', $get_reply_id)->with('cat', $cat)->with('post_owner_info', $post_owner_info);
}

这是我的 Post 模型:

   class Post extends Eloquent 
{
    public function User()
    {
        return $this->belongsTo('User');
    }

    public function Categories()
    {
        return $this->belongstomany('Category');
    }
    public function comments()
    {
        return $this->hasMany('Comment');
    }
}

【问题讨论】:

  • 您能向我们展示您的完整控制器和模型吗?
  • 你试过了吗:$new_post_inst->increment('visit', 1);

标签: php mysql laravel eloquent increment


【解决方案1】:

你可以这样试试

$new_post_inst = Post::where('id','=',$id)->first();
$new_post_inst->increment('visit',1);

【讨论】:

  • 我试过了:$new_post_inst->increment('visit',1);现在,但这不是答案。
  • 我的数据库中保存了 75 次访问,但在我之后问题仍然存在,新的我的数据库中有 77 次访问。
  • 你能检查一下是否有双重重定向吗?
  • 如果有的话如何找到?
【解决方案2】:

试试这个:

$new_post_inst = Post::where('id','=',$id)->first();
$new_post_inst->visit+=1;
$new_post_inst->save();

或者这个:

$new_post_inst = Post::where('id','=',$id)->first();
$new_post_inst->update(array('visit' => $new_post_inst->visit+1));

【讨论】:

  • 不幸的是,没有一个主题工作。我不知道为什么它总是增加2!!!!请帮忙。
【解决方案3】:

我四处搜索了一下,这可能是正在发出第二个请求的网站图标。你是如何声明网站图标的?或者您可以将其删除以尝试查看它是否确实是网站图标?

【讨论】:

    猜你喜欢
    • 2016-06-01
    • 2015-08-20
    • 2020-12-13
    • 1970-01-01
    • 2019-08-10
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    相关资源
    最近更新 更多