Steve Steve - 6 months ago 15
MySQL Question

url value not changing through loop

I am trying to display the comments by the user from the database and +1 could be given to their comment. this is the view.

<br>Comments:<br>
@foreach($show as $sh)
<span class="comment">{{$sh->comment}} --<b style="color:blue">{{$sh->name}},</b> {{$sh->email}}</span>
<form method="POST" action="{{url("/review/now/add/{$sh->id}")}}">
{!! csrf_field() !!}<input type="submit" value="+1">
Total score: <mark>{{$sh->score}}</mark><br><br>
@endforeach


but i am redirected to
/review/now/add/1
with every +1 button i click. I am supposed to +1 the comment i click and store it's score in database.
comment
,
name
,adn
score
are the database columns.

Route:

Route::post('/review/now/add/{id}','reviewcontroller@nowadd');


Controller:

public function nowadd($id)
{
$add=review::where('id',$id)->get();
$count=$add->score;
$now=$count+1;
$nowadd= review::where('id',$id)->update(['score'=>$now]);
return redirect('/review')
->with('message','your have +1 d the comment.' );
}


with that where clause i am supposed to select the row of that id in the url and add +1 to the score of that id. but i am failing here too. it displays undefined property score. the $add might be an array which is causing this error. but how could it be? i am selecting only one row with that id. And when i use
first();
instead of
get();
everything is fine but only for 1st comment.

Answer

TRY THIS

public function nowadd($id)
  {
        review::where('id', $id)->increment('score',1);  // assuming that score is a collumn in your review database
        return redirect('/review')
        ->with('message','your have +1 d the comment.' );      
  }

UPDATE : close the form

 <br>Comments:<br>
    @foreach($show as $sh)
    <span class="comment">{{$sh->comment}} --<b style="color:blue">{{$sh->name}},</b> {{$sh->email}}</span>
       <form method="POST" action="{{url("/review/now/add/{$sh->id}")}}">
         {!! csrf_field() !!}<input type="submit" value="+1">
       </form> 
    Total score: <mark>{{$sh->score}}</mark><br><br>
    @endforeach