douqin6785 2014-11-11 15:42
浏览 357
已采纳

Laravel投票系统

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');
  • 写回答

2条回答 默认 最新

  • duan4523 2014-11-11 16:01
    关注

    change

    <a href="/vote/{{ $project->id }}">Vote</a>
    

    to

    <a href="/projects/vote/{{ $project->id }}">Vote</a>
    

    p.s. if you have no validation in place and if you didn't setup a unique index in database with project id and user id, a student can vote multiple times for one particular project.... which you probably you won't want perhaps?

    Edit (using fluent query builder)

    Route::get('/projects', 'ProjectController@showall'); will show all projects.

    if you want to get the detail for each of the project, you will have to do:

    • set up a route. e.g. Route::get('/projects/{id}', 'ProjectController@showone')
    • then retrieve the result in the controller/repo/model
      return DB::table('vote')->where('project_id','=',$id)->get();

    if you want to show each vote count for every project, then it will be little bit complicated but here's this.

    return DB::table('project AS p')->select([DB::raw('@project:=p.id'), 
           DB::raw('(SELECT COUNT(id) FROM votes AS v WHERE v.project_id = @project) AS votes'),
           ])->paginate(30); //or get()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?