How to make a Like button with counter using laravel 5.3?

0

I'm having trouble running the Counter for the likes and dislikes. The problem is that you are not saving the numbers in the bank.

For a better understanding, here are the details:

Layout

NewsController.php

publicfunctionAdicionarGostei(){$id=Input::get('noticia_id');$noticia=Noticias::find($id);if(Cache::has('voto'.$id)==false){Cache::add('voto'.$id,'contador',0.30);$noticia->total_gostei+=1;$noticia->save();return\Response::json(array('status'=>'sim','qtde'=>$noticia->total_gostei));}else{return\Response::json(array('status'=>'nao'));}}publicfunctionAdicionarNaoGostei(){$id=Input::get('noticia_id');$noticia=Noticias::find($id);if(Cache::has('voto'.$id)==false){Cache::add('voto'.$id,'contador',0.30);$noticia->total_naogostei+=1;$noticia->save();return\Response::json(array('status'=>'sim','qtde'=>$noticia->total_naogostei));}else{return\Response::json(array('status'=>'nao'));}}

Route

Route::post('adicionar-gostei','NoticiaController@AdicionarGostei');Route::post('adicionar-naogostei','NoticiaController@AdicionarNaoGostei');

View

 <div class="caption" style="height: 40px; padding: 5px" >
                    <p style="text-align: center">
                        <a class="btn  btn-success _btnGostei" role="button">Gostei <i class="glyphicon glyphicon-thumbs-up"> ({{$noticia->total_gostei}})</i> </a>
                        <a class="btn  btn-warning _btnNaoGostei" role="button">Não Gostei <i class="glyphicon glyphicon-thumbs-down"> ({{$noticia->total_naogostei}})</i></a>
                    </p>
                </div>

                {{csrf_field()}}  
                <input type="hidden" name="noticia_id" value="{{$noticia->id}}">
                <script type="text/javascript">
                    $("._btnGostei").click(function () {
                        $.noticia("/adicionar-gostei",{noticia_id:$('input[name="noticia_id"]').attr("value"),_token:$('input[name="_token"]').attr("value")},function (response) {
                            if(response.status=="sim")
                            {
                                $("._btnGostei").html("Gostei ("+response.qtde+")");
                            }
                            
                        });

                    });

                    $("._btnNaoGostei").click(function () {
                        $.noticia("/adicionar-naogostei",{noticia_id:$('input[name="noticia_id"]').attr("value"),_token:$('input[name="_token"]').attr("value")},function (response) {
                            if(response.status=="sim")
                            {
                                $("._btnNaoGostei").html("Não Gostei ("+response.qtde+")");
                            }

                        });

                    });
                </script>
    
asked by anonymous 17.04.2018 / 00:57

0 answers