I am implementing a voting system in my laravel school project, it will be an online school platform where students can upload projects and other students can vote on the published content.
Somehow what I have done isn't working... Here is my code so far, hope someone can help me, and maybe help me on the way how to show the number of votes on a project.
This is my database migration:
Schema::create('votes', function($table){
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('project_id')->unsigned();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('project_id')->references('id')->on('projects');
});
My Model:
Class Vote extends Eloquent{
public function user(){
return $this->belongsTo('User');
}
public function project(){
return $this->belongsTo('Project');
}
}
My view:
@foreach ($projects as $project)
<li class="medium-4 columns">
<div class="canvas-medium">
<img src="{{ $project->image }}">
</div>
<p class="title">{{ $project->name }}</p>
<p class="owner">By
<a href="profile/{{ $project->user_id }}">{{ $project->user->firstname }} {{ $project->user->name }}</a>
<a href="/vote/{{ $project->id }}">Vote</a>
</p>
</li>
@endforeach
My controller:
class VoteController extends BaseController{
public function vote($id){
$vote = new Vote;
$vote->user_id = Auth::id();
$vote->project_id = $id;
$vote->Save();
return Redirect::to('/projects');
}
}
Routes.php:
Route::get('/projects', 'ProjectController@showall');
Route::get('/projects/vote/{id}', 'VoteController@vote');