Laravel 5.0 使用 bootstrap3 形式插入数据

Laravel 5.0 data Insertion using bootstrap3 form

我正在尝试使用 bootstrap 表单将数据插入到我的数据库名称 Client 中。但是,如果我单击提交按钮,该页面将直接定向到 newClinetForm 页面,而不会向客户端数据库中插入任何值。我正在使用 laravel 版本 5.0.

这是 newClientForm blade 模板的表单部分:

<form role="form" method="post" action="{{url('clients/newClientForm')}}">
    <input type="hidden" name="_token" value="{{csrf_token() }}">

    <div class="form-group">
        <label>Name</label>
        <input class="form-control" name="ClientName" placeholder="Erfan">
    </div>

    <div class="form-group">
        <label>Client ID</label>
        <input class="form-control" name="ClientID" placeholder="Ect112">
    </div>

    <div class="form-group">
        <label>Phone No.</label>
        <input class="form-control" name="Phone" placeholder="01XX-XXXXXXX">
    </div>
    <button type="submit" class="btn btn-default">Submit Button</button>
</form>

我尝试了 Session::token() 而不是 csrf_token(),但也没有帮助。

这是我的控制器部分:

public function newClientFormIndex(){
    return view('clients.newClientForm');
}

public function store(Request $request) {
    $client = new Client();
    $client->ClientID = $request->ClientID;
    $client->ClientName = $request->ClientName;
    $client->Phone = $request->Phone;
    $client->save();
}

这是模型部分:

class Client extends Model {
    protected $table = 'clients';
    public $timestamps = false;
    protected $primaryKey = 'ClientID';
    protected $fillable = ['ClientID', 'ClientName', 'Phone'];
}

这是路线部分:

Route::resource('clients/newClientForm', 'IspController@newClientFormIndex');

我找不到错误的地方。

newClientFormIndex 中,您没有调用 store 函数。 此外,区分显示表单的路由和实际保存数据的路由可能是值得的,例如:

Route::get('clients/newClientForm', 'IspController@newClientFormIndex');
Route::post('clients/newClientForm', 'IspController@store');

然后更改您的存储方法以重定向到表单:

public function store(Request $request) {
    $client = new Client();
    $client->ClientID = $request->ClientID;
    $client->ClientName = $request->ClientName;
    $client->Phone = $request->Phone;
    $client->save();
    return redirect('clients/newClientForm');
}

这是为了避免重新提交表单。