laravel eloquent : Increments visits column by one for every visits












1















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









share|improve this question

























  • 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
















1















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









share|improve this question

























  • 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














1












1








1








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









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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



















  • 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












3 Answers
3






active

oldest

votes


















2














you can try like this



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





share|improve this answer
























  • 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



















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





share|improve this answer
























  • 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



















1














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?






share|improve this answer























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


    }
    });














    draft saved

    draft discarded


















    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









    2














    you can try like this



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





    share|improve this answer
























    • 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
















    2














    you can try like this



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





    share|improve this answer
























    • 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














    2












    2








    2







    you can try like this



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





    share|improve this answer













    you can try like this



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






    share|improve this answer












    share|improve this answer



    share|improve this answer










    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



















    • 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













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





    share|improve this answer
























    • 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
















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





    share|improve this answer
























    • 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














    1












    1








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





    share|improve this answer













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






    share|improve this answer












    share|improve this answer



    share|improve this answer










    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



















    • 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











    1














    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?






    share|improve this answer




























      1














      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?






      share|improve this answer


























        1












        1








        1







        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?






        share|improve this answer













        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?







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jun 23 '15 at 9:17









        EricH206EricH206

        526410




        526410






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            How to send String Array data to Server using php in android

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

            Is anime1.com a legal site for watching anime?