2016-10-21 59 views
0

我創建了一個表格,我試圖提交數據,但錯誤:MethodNotAllowedHttpException在RouteCollection.php行218:試圖提交數據

MethodNotAllowedHttpException in RouteCollection.php line 218: 

即將到來。該怎麼辦?

歡迎刀片:

@extends('layouts.master') 

@section('title') 
Welcome!! 
@endsection 

@section('content') 
@include('includes.message-block') 
<div class="row"> 
    <div class="col-md-6"> 
     <h2>Enter Your Information</h2> 
      <form action="" method="post">  
      <div class="form-group" {{$errors -> has('name') ? 'has-error': ''}}> 
       <label for="name">Name</label> 
       <input class="form-control" type="text" name="name" id="name" placeholder="Your name"> 
      </div> 
      <div class="form-group" {{$errors -> has('email') ? 'has-error': ''}}> 
       <label for="email">Email-Id</label> 
       <input class="form-control" type="email" name="email" id="email" placeholder="Enter Your Email Id"> 
      </div> 
      <div class="form-group"> 
       <label for="branch">Branch</label> 
       <input class="form-control" type="text" name="branch" id="branch" placeholder="Enter Your Branch"> 
      </div> 
      <div class="form-group"> 
       <label for="course_name">Course Name</label> 
       <input class="form-control" type="text" name="course_name" id="course_name" placeholder="Enter Your Course"> 
      </div> 
      <button type="submit" class="btn btn-primary">Submit</button> 
      <input type="hidden" name="_token" value="{{Session::token()}}">     
     </form> 
    </div> 
@endsection 

型號:

<?php 

namespace App; 
use Illuminate\Contracts\Auth\Authenticatable; 
use Illuminate\Database\Eloquent\Model; 


class User extends Model implements Authenticatable 
{ 
use \Illuminate\Auth\Authenticatable; 
protected $table = 'students'; 

} 

UserController中:

<?php 

namespace App\Http\Controllers; 
use Illuminate\Support\Facades\Input; 
//use Illuminate\Support\Facades\Flash; 
use InvalidConfirmationCodeException; 
use Flash; 
//use Mail; 
use App\User; 
use Illuminate\Http\Request; 
use Illuminate\Support\Facades\Auth; 
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersstudents; 

class UserController extends Controller 

{ 

public function postData(Request $request) 
{ 
    $this -> validate($request,[ 
     'email' => 'required|email|unique:students', 
     'name' => 'required|max:20', 
     'branch' => 'required|max:10' 
     'course_name' => 'required|max:10' 
     ]); 

    $email = $request['email']; 
    $name = $request['name']; 
    $branch = $request['branch']; 
    $course_name = $request['course_name']; 



    $student = new User(); 
    $student->email =$email; 
    $student->name = $name; 
    $student->branch = $branch; 
    $student->course_name = $course_name; 


    $student->save(); 

    return redirect()->back(); 

} 
} 

路線/ web.php:

Route::group(['middleware' => ['web']], function(){ 
Route::get('/', function() { 
    return view('welcome'); 
}); 

Route::post('/submit_data',[ 
    'uses' => '[email protected]', 
    'as' => 'submit_data' 
    ]); 
}); 

遷移:

<?php 

use Illuminate\Support\Facades\Schema; 
use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

class CreateUsersTable extends Migration 
{ 

public function up() 
{ 
    Schema::create('students', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->string('mail')->unique(); 
     $table -> string('name'); 
     $table -> string('branch'); 
     $table -> string('course_name'); 
     $table->rememberToken(); 
     $table->timestamps(); 
    }); 
} 


public function down() 
{ 
    Schema::drop('students'); 
} 
} 

我的遷移名爲create_users_table。我不明白爲什麼會出現這個錯誤。這是一種簡單易用的形式。只需填寫一份簡單的表格即可提交學生數據,並將其保存在數據庫中。

+0

你'action'支撐在'<形式行動=「」方法=「郵報」 >'是空的,你應該嘗試添加你配置的路由:'

' – Hammerbot

回答

0
<form action="{{ url('submit_data') }}" method="post"> 
1

您發佈到get路線

在你welcome.blade.php你忘了把action,所以默認情況下它會嘗試發佈到同一頁/其中的肺癌是一個get路線在你的routes/web.php。動作設置爲/submit_data,像這樣在你的welcome.blade.php

<form action="{{ url('/submit_data') }}" method="post"> 
+0

謝謝,我這樣做了,但是另一個錯誤:QueryException在Connection.php第761行: SQLSTATE [42S02]:未找到基本表或視圖:1146表'larw.students'不存在(SQL:select count(*)as'students'中的聚合,其中'email' = shefali195sha [email protected]) –

+0

我已將我的數據庫更改爲「數據庫」,即我的數據庫的名稱。 larw是我的舊數據庫。我不明白爲什麼當我更改數據庫的名稱時會出現此錯誤。 –

+0

由於錯誤狀態'Table'larw.students'不存在',所以查詢的表不存在。你有沒有運行你的遷移? –

0

你也可以用刀片從這樣

{{Form::open(array('url'=>'/submit_data','files'=>true,'class'=>'form-horizontal','file'=>'true'))}} 
相關問題