2016-11-25 36 views
2

我正在使用laravel框架进行投票系统,但是现在我遇到了一些问题。我想将用户选择的项目编号传递到另一个视图(同一控制器下的所有项目)。在其他项目中,我使用Input :: get(),但现在有些东西不工作。我得到MethodNotAllowedHttpException在RouteCollection.php行218: 这是我的看法projecs_list部分,Laravel投票系统获取用户选择的项目数

`

@foreach($project as $key => $value) ` 
    <form class="form-horizontal" role="form" method="POST" action="{{ url('/vote'}}" > 
     <div class="row banner"> 

      <div class="col-md-8 col-md-offset-2 "> 

       <div class="project_nr_5" name="5" > 
       <h3 class="text-primary text-center"> 
       {{ $value->tytuł}} 
       </h3> 
       <p class="lead"> 
       {{ $value->opis}} 
       </p> 
       <p class="lead"> 
       {{ $value->lokalizacja }} 
       </p> 
       <p name="project_id" id="project_id"> Projekt numer: {{ $value->id}} </p> 
       <button type="submit" value=" {{ $value->id }} " class="btn btn-primary center-block" > 
       ZAGŁOSUJ 
       </button> 
       </a> 
       </div> 
      </div> 

      </div> 
     </form> 
     @endforeach 

在这里,我想通过PROJECT_ID值。

这里有路线

`

Route::get('/projekty','[email protected]_projects'); 
Route::get('/vote','[email protected]'); 
Route::post('/store','[email protected]'); 
` 
` 

这里是控制器

`

<?php 

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 

use App\Http\Requests; 
use App\Voter; 
use App\Vote; 
use App\SingleProject; 
use Illuminate\Support\Facades\Input; 
class MakeVoteController extends Controller 
{ 
    /** 
    * Display a listing of the resource. 
    * 
    * @return \Illuminate\Http\Response 
    */ 
    public function index() 
    { 
     // 
     //$project_id = Input::get('project_id'); 

     //number of the projet the user choosed 
     $selected_project = Input::get('project_id'); 
     return view('vote') 
     ->with('selected_project', $selected_project); 

    } 

    /** 
    * Show the form for creating a new resource. 
    * 
    * @return \Illuminate\Http\Response 
    */ 
    public function create() 
    { 
     // 
    } 

    /** 
    * Store a newly created resource in storage. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @return \Illuminate\Http\Response 
    */ 
    public function store(Request $request) 
    { 
     // Save data to table Voters, get values from submit of vote form 

     // $data= Input::all(); 


     $this->validate($request, [ 
      'pesel' =>'required|unique|max:11|min:11', 
      'name' =>'required|max:64|min:2', 
      'surname' => 'required|max:128|min:2' 
      ]); 

     $newvoter = new Voter; 
     $newvoter->name = Input::get("name"); 
     $newvoter->surname = Input::get("surname"); 
     $newvoter->pesel = Input::get("pesel"); 
     $newvoter->save(); 

     $givevote = new Vote; 





     return "Successfull adding to database"; 



    } 

    /** 
    * Display the specified resource. 
    * 
    * @param int $id 
    * @return \Illuminate\Http\Response 
    */ 
    public function show($id) 
    { 
     // 
    } 

    /** 
    * Show the form for editing the specified resource. 
    * 
    * @param int $id 
    * @return \Illuminate\Http\Response 
    */ 
    public function edit($id) 
    { 

    } 

    /** 
    * Update the specified resource in storage. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @param int $id 
    * @return \Illuminate\Http\Response 
    */ 
    public function update(Request $request, $id) 
    { 
     // 
    } 

    /** 
    * Remove the specified resource from storage. 
    * 
    * @param int $id 
    * @return \Illuminate\Http\Response 
    */ 
    public function destroy($id) 
    { 
     // 
    } 

    public function show_projects() 
    { 
     $projects = SingleProject::all(); 

     return view('projects_list') 
      ->with('project', $projects); 
     $proj_select_id = Input::get("project_id"); 
     index($proj_select_id); 
    } 



} 

`

,并在去年这里的投票视图,其中用户填写表格并为选定的项目投票。

`

<div class="panel panel-default"> 
       <div class="panel-heading"> Zagłosuj na projekt nr {{ $selected_project }}</div> 
       <div class="panel-body"> 
        <form class="form-horizontal" role="form" method="POST" action="{{ url('/store') }}"> 

        {{ csrf_field() }} 

        <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}"> 
         <label for="name" class="col-md-4 control-label">Imie</label> 
         <div class="col-md6"> 
          <input id="name" maxlength="255" type="text" class="form-control" name="name" required autofocus> 
          @if ($errors->has('name')) 
           <span class='help-block'> 
            <strong>{{ $errors->first('name') }}</strong> 
           </span> 
          @endif 
         </div> 
        </div> 

        <div class="form-group{{ $errors->has('surname') ? ' has-error' : '' }}"> 
          <label for="surname"class="col-md-4 control-label">Nazwisko</label> 
          <div class="col-md6"> 
           <input id="surname" type="text" class="form-control" name="surname" required> 

           @if ($errors->has('surname')) 
            <span class="help-block"> 
             <strong>{{ $errors->first('surname') }} </strong> 
            </span> 
           @endif 
          </div> 
         </div> 
         <div class="form-group{{ $errors->has('pesel') ? ' has-error' : '' }}"> 
          <label for="pesel"class="col-md-4 control-label">pesel</label> 
          <div class="col-md6"> 
           <input id="pesel" type="text" class="form-control" name="pesel" required maxlength="11" minlength="11"> 

           @if ($errors->has('pesel')) 
            <span class="help-block"> 
             <strong>{{ $errors->first('pesel') }} </strong> 
            </span> 
           @endif 
          </div> 
         </div> 
         <div class="form-group{{ $errors->has('street') ? ' has-error' : '' }}"> 
          <label for="street"class="col-md-4 control-label">Ulica</label> 
          <div class="col-md6"> 
           <input id="street" type="text" class="form-control" name="street" required > 

           @if ($errors->has('pesel')) 
            <span class="help-block"> 
             <strong>{{ $errors->first('pesel') }} </strong> 
            </span> 
           @endif 
          </div> 
         </div> 

        <div class="form-group"> 
          <div class="col-md-6 col-md-offset-4"> 
           <button type="submit" class="btn btn-primary"> 
            Zagłosuj 
           </button> 
          </div> 
         </div> 
         </div> 

        </form> 
       </div> 
+0

请指出第218行。 – Carcigenicate

+0

RouteCollection行:218是受保护的函数methodNotAllowed(array $ others) { throw new MethodNotAllowedHttpException($ others); } –

+0

请在问题中包含该代码及其周围的代码。 – Carcigenicate

回答

0

在你project_list视图定义形式attribut method="POST" action="{{ url('/vote'}}"

但在你的路线定义/vote以http GET方法:Route::get('/vote','[email protected]');

他们应该是在同一个HTTP方法。

+0

取决于我是否点击href(然后我得到TokenMismatchException)或写手(localhost/vote,在这里我得到MethodNotAllowed ...) –

+0

如果你想要在你的'project_list'表格中添加'{{csrf_field()}}''使用'POST'方法。 –

+0

可悲的是什么都没有。我在表单之前添加了{{csrd_field()}},但我仍然得到TokenMismatchException –