I've built a forum in Laravel which comes with voting functions. The votes are +/-1. An attribute then counts all the 'upvotes' and 'downvotes' and displays them accordingly. All works fine locally with the votes displaying but as soon as I take it live on our server, the count doesn't seem to change.
The form:
{!! Form::open(['route' => 'votes.store']) !!}
{!! Form::button('<i class="fa fa-thumbs-o-up"></i>', array('type' => 'submit', 'value' => 'vote', 'name' => 'vote')) !!}
{!! Form::hidden('task_id', $task->id) !!}
{{$task->upvotes}}
{!! $task->upvotes !!}
{!! Form::button('<i class="fa fa-thumbs-o-down"></i>', array('type' => 'submit', 'value' => 'downvote', 'name' => 'downvotes')) !!}
{{$task->downvotes}}
{!! Form::hidden('task_id', $task->id) !!}
{!! Form::close() !!}
The controller:
public function store(Request $request){
$this->validate($request, ['vote'])
$input = new Votes($request->all());
// $input->user()->associate($request->user());
// $input->tasks()->associate($request->get('task_id'));
if($request->get('downvotes')){
$input->vote = -1;
}
else {
$input->vote = 1;
}
$input->save();
// dd($request->get('upvotes'));
return redirect()->back();
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$task = Task::findOrFail($id);
$categories = Category::findOrFail($id);
$votes = Votes::whereTaskId($task);
return view('tasks.show')->withTask($task)->withCategories($categories)->withVotes($votes);
}
The model relations are fine and all is showing in the SQL DB. Here are the attributes too:
public function getUpvotesAttribute()
{
return $this->votes->where('vote', 1)->count();
}
public function getDownvotesAttribute()
{
return $this->votes->where('vote', -1)->count();
}
I'm not entirely sure what would be causing the problem and if I can provide any more details/clarify anything do let me know. I apologise if this is unclear.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire