laravel eloquent : Increments visits column by one for every visits
I have model for my post in my site.My post model has a column that stores visits.when user visit my post should increments by 1.
I exactly knows how to do this, but my problem is when I increment it by one in my model, it's increments by 2!!!!!
I wrote this code in my controller:
$new_post_inst = Post::where('id','=',$id)->first();
$new_post_inst->increment('visit');
This is my 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);
}
And this is my Post model:
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');
}
}
php mysql laravel eloquent increment
add a comment |
I have model for my post in my site.My post model has a column that stores visits.when user visit my post should increments by 1.
I exactly knows how to do this, but my problem is when I increment it by one in my model, it's increments by 2!!!!!
I wrote this code in my controller:
$new_post_inst = Post::where('id','=',$id)->first();
$new_post_inst->increment('visit');
This is my 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);
}
And this is my Post model:
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');
}
}
php mysql laravel eloquent increment
can you show us your full controller and model to?
– Safoor Safdar
Jun 23 '15 at 7:17
Have you tried : $new_post_inst->increment('visit', 1);
– Sajal
Jun 23 '15 at 7:19
add a comment |
I have model for my post in my site.My post model has a column that stores visits.when user visit my post should increments by 1.
I exactly knows how to do this, but my problem is when I increment it by one in my model, it's increments by 2!!!!!
I wrote this code in my controller:
$new_post_inst = Post::where('id','=',$id)->first();
$new_post_inst->increment('visit');
This is my 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);
}
And this is my Post model:
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');
}
}
php mysql laravel eloquent increment
I have model for my post in my site.My post model has a column that stores visits.when user visit my post should increments by 1.
I exactly knows how to do this, but my problem is when I increment it by one in my model, it's increments by 2!!!!!
I wrote this code in my controller:
$new_post_inst = Post::where('id','=',$id)->first();
$new_post_inst->increment('visit');
This is my 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);
}
And this is my Post model:
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');
}
}
php mysql laravel eloquent increment
php mysql laravel eloquent increment
edited Jun 23 '15 at 7:24
Amir H
asked Jun 23 '15 at 7:16
Amir HAmir H
6271722
6271722
can you show us your full controller and model to?
– Safoor Safdar
Jun 23 '15 at 7:17
Have you tried : $new_post_inst->increment('visit', 1);
– Sajal
Jun 23 '15 at 7:19
add a comment |
can you show us your full controller and model to?
– Safoor Safdar
Jun 23 '15 at 7:17
Have you tried : $new_post_inst->increment('visit', 1);
– Sajal
Jun 23 '15 at 7:19
can you show us your full controller and model to?
– Safoor Safdar
Jun 23 '15 at 7:17
can you show us your full controller and model to?
– Safoor Safdar
Jun 23 '15 at 7:17
Have you tried : $new_post_inst->increment('visit', 1);
– Sajal
Jun 23 '15 at 7:19
Have you tried : $new_post_inst->increment('visit', 1);
– Sajal
Jun 23 '15 at 7:19
add a comment |
3 Answers
3
active
oldest
votes
you can try like this
$new_post_inst = Post::where('id','=',$id)->first();
$new_post_inst->increment('visit',1);
I have tried :$new_post_inst->increment('visit',1); right now but that's not the answer.
– Amir H
Jun 23 '15 at 7:27
what shows after execute it.
– Imtiaz Pabel
Jun 23 '15 at 7:29
I have 75 visits saved in my DB, But after I it still the problem remains and new I have 77 visits in my DB.
– Amir H
Jun 23 '15 at 7:31
can you please check is there double redirection?
– Imtiaz Pabel
Jun 23 '15 at 7:33
How can I find if I have?
– Amir H
Jun 23 '15 at 7:34
add a comment |
Try this:
$new_post_inst = Post::where('id','=',$id)->first();
$new_post_inst->visit+=1;
$new_post_inst->save();
OR this:
$new_post_inst = Post::where('id','=',$id)->first();
$new_post_inst->update(array('visit' => $new_post_inst->visit+1));
Unfortunately none of theme working. I don't know why it always increments by two!!!! Please help.
– Amir H
Jun 23 '15 at 9:48
add a comment |
I searched around for a little bit and this could be the favicon icon that is making a second request. How did you declare the favicon? Or maybe you could remove it to try and see if it is indeed the favicon?
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f30996216%2flaravel-eloquent-increments-visits-column-by-one-for-every-visits%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
you can try like this
$new_post_inst = Post::where('id','=',$id)->first();
$new_post_inst->increment('visit',1);
I have tried :$new_post_inst->increment('visit',1); right now but that's not the answer.
– Amir H
Jun 23 '15 at 7:27
what shows after execute it.
– Imtiaz Pabel
Jun 23 '15 at 7:29
I have 75 visits saved in my DB, But after I it still the problem remains and new I have 77 visits in my DB.
– Amir H
Jun 23 '15 at 7:31
can you please check is there double redirection?
– Imtiaz Pabel
Jun 23 '15 at 7:33
How can I find if I have?
– Amir H
Jun 23 '15 at 7:34
add a comment |
you can try like this
$new_post_inst = Post::where('id','=',$id)->first();
$new_post_inst->increment('visit',1);
I have tried :$new_post_inst->increment('visit',1); right now but that's not the answer.
– Amir H
Jun 23 '15 at 7:27
what shows after execute it.
– Imtiaz Pabel
Jun 23 '15 at 7:29
I have 75 visits saved in my DB, But after I it still the problem remains and new I have 77 visits in my DB.
– Amir H
Jun 23 '15 at 7:31
can you please check is there double redirection?
– Imtiaz Pabel
Jun 23 '15 at 7:33
How can I find if I have?
– Amir H
Jun 23 '15 at 7:34
add a comment |
you can try like this
$new_post_inst = Post::where('id','=',$id)->first();
$new_post_inst->increment('visit',1);
you can try like this
$new_post_inst = Post::where('id','=',$id)->first();
$new_post_inst->increment('visit',1);
answered Jun 23 '15 at 7:21
Imtiaz PabelImtiaz Pabel
3,6581816
3,6581816
I have tried :$new_post_inst->increment('visit',1); right now but that's not the answer.
– Amir H
Jun 23 '15 at 7:27
what shows after execute it.
– Imtiaz Pabel
Jun 23 '15 at 7:29
I have 75 visits saved in my DB, But after I it still the problem remains and new I have 77 visits in my DB.
– Amir H
Jun 23 '15 at 7:31
can you please check is there double redirection?
– Imtiaz Pabel
Jun 23 '15 at 7:33
How can I find if I have?
– Amir H
Jun 23 '15 at 7:34
add a comment |
I have tried :$new_post_inst->increment('visit',1); right now but that's not the answer.
– Amir H
Jun 23 '15 at 7:27
what shows after execute it.
– Imtiaz Pabel
Jun 23 '15 at 7:29
I have 75 visits saved in my DB, But after I it still the problem remains and new I have 77 visits in my DB.
– Amir H
Jun 23 '15 at 7:31
can you please check is there double redirection?
– Imtiaz Pabel
Jun 23 '15 at 7:33
How can I find if I have?
– Amir H
Jun 23 '15 at 7:34
I have tried :$new_post_inst->increment('visit',1); right now but that's not the answer.
– Amir H
Jun 23 '15 at 7:27
I have tried :$new_post_inst->increment('visit',1); right now but that's not the answer.
– Amir H
Jun 23 '15 at 7:27
what shows after execute it.
– Imtiaz Pabel
Jun 23 '15 at 7:29
what shows after execute it.
– Imtiaz Pabel
Jun 23 '15 at 7:29
I have 75 visits saved in my DB, But after I it still the problem remains and new I have 77 visits in my DB.
– Amir H
Jun 23 '15 at 7:31
I have 75 visits saved in my DB, But after I it still the problem remains and new I have 77 visits in my DB.
– Amir H
Jun 23 '15 at 7:31
can you please check is there double redirection?
– Imtiaz Pabel
Jun 23 '15 at 7:33
can you please check is there double redirection?
– Imtiaz Pabel
Jun 23 '15 at 7:33
How can I find if I have?
– Amir H
Jun 23 '15 at 7:34
How can I find if I have?
– Amir H
Jun 23 '15 at 7:34
add a comment |
Try this:
$new_post_inst = Post::where('id','=',$id)->first();
$new_post_inst->visit+=1;
$new_post_inst->save();
OR this:
$new_post_inst = Post::where('id','=',$id)->first();
$new_post_inst->update(array('visit' => $new_post_inst->visit+1));
Unfortunately none of theme working. I don't know why it always increments by two!!!! Please help.
– Amir H
Jun 23 '15 at 9:48
add a comment |
Try this:
$new_post_inst = Post::where('id','=',$id)->first();
$new_post_inst->visit+=1;
$new_post_inst->save();
OR this:
$new_post_inst = Post::where('id','=',$id)->first();
$new_post_inst->update(array('visit' => $new_post_inst->visit+1));
Unfortunately none of theme working. I don't know why it always increments by two!!!! Please help.
– Amir H
Jun 23 '15 at 9:48
add a comment |
Try this:
$new_post_inst = Post::where('id','=',$id)->first();
$new_post_inst->visit+=1;
$new_post_inst->save();
OR this:
$new_post_inst = Post::where('id','=',$id)->first();
$new_post_inst->update(array('visit' => $new_post_inst->visit+1));
Try this:
$new_post_inst = Post::where('id','=',$id)->first();
$new_post_inst->visit+=1;
$new_post_inst->save();
OR this:
$new_post_inst = Post::where('id','=',$id)->first();
$new_post_inst->update(array('visit' => $new_post_inst->visit+1));
answered Jun 23 '15 at 9:10
NileshNilesh
3142415
3142415
Unfortunately none of theme working. I don't know why it always increments by two!!!! Please help.
– Amir H
Jun 23 '15 at 9:48
add a comment |
Unfortunately none of theme working. I don't know why it always increments by two!!!! Please help.
– Amir H
Jun 23 '15 at 9:48
Unfortunately none of theme working. I don't know why it always increments by two!!!! Please help.
– Amir H
Jun 23 '15 at 9:48
Unfortunately none of theme working. I don't know why it always increments by two!!!! Please help.
– Amir H
Jun 23 '15 at 9:48
add a comment |
I searched around for a little bit and this could be the favicon icon that is making a second request. How did you declare the favicon? Or maybe you could remove it to try and see if it is indeed the favicon?
add a comment |
I searched around for a little bit and this could be the favicon icon that is making a second request. How did you declare the favicon? Or maybe you could remove it to try and see if it is indeed the favicon?
add a comment |
I searched around for a little bit and this could be the favicon icon that is making a second request. How did you declare the favicon? Or maybe you could remove it to try and see if it is indeed the favicon?
I searched around for a little bit and this could be the favicon icon that is making a second request. How did you declare the favicon? Or maybe you could remove it to try and see if it is indeed the favicon?
answered Jun 23 '15 at 9:17
EricH206EricH206
526410
526410
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f30996216%2flaravel-eloquent-increments-visits-column-by-one-for-every-visits%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
can you show us your full controller and model to?
– Safoor Safdar
Jun 23 '15 at 7:17
Have you tried : $new_post_inst->increment('visit', 1);
– Sajal
Jun 23 '15 at 7:19