laravel eloquent:每次访问时逐步增加访问次数

时间:2015-06-23 07:16:27

标签: php mysql laravel eloquent increment

我的网站上有我的帖子模型。我的帖子模型有一个存储访问的列。当用户访问我的帖子时应该增加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');
    }
}

3 个答案:

答案 0 :(得分:2)

你可以这样尝试

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

答案 1 :(得分: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 :(得分:1)

我搜索了一下,这可能是第二个请求的favicon图标。你是怎么宣布这个图标的?或者也许你可以删除它来试试看它是否确实是图标?