我创建了一个表单,我正在尝试提交数据,但错误:
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' => 'UserController@postData',
'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 :(得分:1)
您要发布到get
路径
在welcome.blade.php
您忘记设置action
,因此默认情况下,它会尝试发布到同一页/
,该页面被定义为get
路由你的routes/web.php
。在/submit_data
:
welcome.blade.php
<form action="{{ url('/submit_data') }}" method="post">
答案 1 :(得分:0)
<form action="{{ url('submit_data') }}" method="post">
答案 2 :(得分:0)
你也可以像这样使用刀片
{{Form::open(array('url'=>'/submit_data','files'=>true,'class'=>'form-horizontal','file'=>'true'))}}