我尝试使用此语法
填写表单查看:
@extends('layouts.default')
@section('content')
<h1>Create New Authors Page</h1>
{{Form::open(array('url'=>'authors/create','action'=>'AuthorsController@buat'))}}
@include('common.show_error')
<p>
{{Form::label('name', 'Name: ')}}<br>
{{Form::text('name', Input::old('name'))}}
</p>
<p>
{{Form::label('bio','Biography')}}<br>
{{Form::textarea('bio', Input::old('bio'))}}
</p>
<p>{{Form::submit('Add Authors')}}</p>
{{Form::close()}}
@stop
Controller(AuthorsController):
public function buat()
{
Author::create(array(
'name'=>Input::get('name'),
'bio'=>Input::get('bio')
));
return Redirect::route('authors')
->with('message','New Author Succesfully Added');
}
Rotue:
Route::post('authors/create', 'AuthorsController@buat');
它没有错误地传递,但数据库没有填充任何内容......空白而没有任何记录..
但是。当我在控制器中使用这种语法时,它可以工作:
public function buat(){
$authorr = new Author;
$authorr->name = Input::get('name');;
$authorr->bio = Input::get('bio');;
$authorr -> save();
return Redirect::route('authors')
->with('message','New Author Succesfully Added');
}
请帮我用第一种语法填充数据库
答案 0 :(得分:0)
我可以在您使用过的表单中看到:
{{ Form::open(array('url'=>'authors/create','action'=>'AuthorsController@buat')) }}
您应该使用url
或action
,而不是两者:
{{ Form::open(array('url'=>'authors/create')) }}
或者tthis:
{{ Form::open(array('action'=>'AuthorsController@buat')) }}
顺便说一句,你可以用这个:
$author = Author::create(array(
'name'=>Input::get('name'),
'bio'=>Input::get('bio')
));
if($author) {
return Redirect::route('authors')
->with('message', 'New Author Succesfully Added');
}
但请确保您在$fillable
模型中声明了Author
数组,如:
protected $fillable = array('name', 'bio');
A;因此您可以声明一个空的$guarde
数组而不是$fillable
来允许任何字段进行质量分配,如下所示:
protected $guarded = array();
此外,通过设置此项来启用app/config/app.php
文件中的调试非常有用:
debug => true,
因此,您将获得提供信息的错误消息。