首页 > 解决方案 > 在laravel中单击按钮时如何增加DB中的计数值?

问题描述

当我单击该页面中的投票按钮时,我有Home.blade.php ,候选表中的计数值应该增加。

这是我的Home.blade.php

@foreach ($file as $show)
            <div class="col-md-3">
                <div class="card">
                      <img src="p3.png" alt="John" style="width:100%">
                          <div class="card-body">
                              <h5 class="title">President</h5>
                              <p class="card-text">Name : {{$show->name}}</p>
                               <!--  <p><button>VOTE</button></p> -->
                                 <button type="button" class="vote">VOTE</button>
                          </div>
                </div>
            </div>
      @endforeach

我有一个名为候选人的表名,当点击投票按钮时,数据库中的计数应该增加。我有一个叫做HomeController请帮助我的控制器。谢谢

class Homecontroller extends Controller
{
    public function pre()
    {
         $file = DB::table('candidate')
                     ->select('name','branch')
                     ->where('candidate.post_id', '=', 1)
                     ->get();

        return view('/home')->with('file',$file);

     }
}



@foreach ($file as $show)
            <div class="col-md-3" style="margin-bottom: 20px;">
                <div class="card">
                      <img src="p3.png" alt="John" style="width:100%">
                          <div class="card-body">
                              <h5 class="title">President</h5>
                              <p class="card-text">Name : {{$show->name}}</p>

                                 <a href="{{url("Homecontroller@count")}}"><p><button>VOTE</button></p> </a>
                          </div>
                </div>
            </div>
      @endforeach

我已经使用函数重定向到控制器

在控制器中我使用了这个功能

        public function Count(){
        DB::table('candidate')->increment('count',1);
        return view('/home');
        }

仍然无法正常工作

这是我的路线

Route::get('/home','Homecontroller@count');

标签: ajaxlaravel

解决方案


推荐阅读