gpt4 book ai didi

php - laravel Eloquent : Increments visits column by one for every visits

转载 作者:行者123 更新时间:2023-11-29 04:07:01 25 4
gpt4 key购买 nike

我的网站上有我的帖子模型。我的帖子模型有一个存储访问的列。当用户访问我的帖子时,应该递增 1。我完全知道该怎么做,但我的问题是当我在我的模型中将它递增 1 时,它递增 2!!!!!

我在我的 Controller 中写了这段代码:

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

这是我的 Controller :

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

关于php - laravel Eloquent : Increments visits column by one for every visits,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30996216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com