簡體   English   中英

laravel雄辯:每次訪問都會增加訪問次數一欄

[英]laravel eloquent : Increments visits column by one for every visits

我的網站上有我的帖子的模型。我的帖子模型有一個存儲訪問量的列。當用戶訪問我的帖子時,該帖子應增加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 = Post::where('id','=',$id)->first();
$new_post_inst->increment('visit',1);

嘗試這個:

$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));

我搜索了一下,這可能是發出第二個請求的收藏夾圖標。 您如何聲明收藏夾圖標? 或者,您可以將其刪除以嘗試查看它是否確實是favicon?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM