当尝试使用雄辩的ORM将条目存储在我的数据库中时,出现错误,
Illuminate\Database\QueryException thrown with message "SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value (SQL: insert into `animals` (`updated_at`, `created_at`) values (2019-04-25 22:35:47, 2019-04-25 22:35:47))"
但是,在我的迁移中,我从未根据需要将名称指定为默认名称。
我的AnimalsController类。
public function create()
{
return view('animals.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$animal = Animal::create([
$request->input()
]);
// $animal = new Animal;
// $animal->name = $request->name;
// $animal->dob = $request->dob;
// $animal->description = $request->description;
// $animal->image = $request->image;
// $animal->available = $request->available;
//
// echo $request->id;
// echo "test";
// echo $animal->id;
// flash('Animal Added')->success();
return redirect()->route('animals.show')->with('animal', $animal);
}
/**
* Display the specified resource.
*
* @param \App\Animal $animal
* @return \Illuminate\Http\Response
*/
public function show(Animal $animal)
{
return view('animals.show')->with('animal', $animal);
}
Animal.php
class Animal extends Model
{
use SoftDeletes;
protected $dates = [
'created_at',
'updated_at'
];
/**
* Set the Attributes which are alllowed
* to be assigned
*/
protected $fillable = [
'name',
'dob',
'description',
'image',
'available'
];
create.blade.php-输入表单数据的地方
<div class="row">
<div class="col">
{!! Form::open(['route' => 'animals.store'], ['class' => 'form', 'files' => true]) !!}
<div class="form-group">
{!! Form::label('name', 'Animal Name', ['class' => 'control-label']) !!}
{!! Form::text('name', null, ['class' => 'form-control input-lg', 'placeholder' => 'Waffles']) !!}
</div>
<div class="form-group">
{!! Form::label('dob', "DOB", ['class' => 'control-label']) !!}
{!! Form::date('dob') !!}
</div>
<div class="form-group">
{!! Form::label('description', "Description", ['class' => 'control-label']) !!}
{!! Form::textarea('description', null, ['size' => '20x3', 'class' => 'form-control input-lg', 'placeholder' => 'Describe the animal']) !!}
</div>
<div class="form-group">
{!! Form::label('image', "Image", ['class' => 'control-label']) !!}
{!! Form::file('image') !!}
</div>
<div class="form-group">
{!! Form::label('available', "Available", ['class' => 'control-label']) !!}
{!! Form::checkbox('available', 'value') !!}
</div>
<div class="form-group">
{!! Form::submit('Add Animal', ['class' => 'submit btn btn-info btn-lg', 'style' => 'width: 100%']) !!}
</div>
{!! Form::close() !!}
</div>
CreateAnimalsTable迁移
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAnimalsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('animals', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 20);
$table->date('dob');
$table->mediumText('description');
$table->string('image');
$table->boolean('available')->default(false);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('animals');
}
}
以防我的show.blade.php文件值得任何东西。
<div class="container">
<div class="row">
<div class="col-xl-12 text-center">
<p class="text-primary" style="margin-top:100px;">{{ $animal->name }} has animal ID #{{ $animal->id }}. Date: {{ $animal->dob }}</p>
</div>
</div>
</div>
我已经玩了好几个小时试图使它生效,但我对解决方案视而不见。
我可能还应该提到如果我使用此代码
public function store(Request $request)
{
// $animal = Animal::create([
// $request->input()
// ]);
$animal = new Animal;
$animal->name = $request->name;
$animal->dob = $request->dob;
$animal->description = $request->description;
$animal->image = $request->image;
$animal->available = $request->available;
echo $request->id;
echo "test";
echo $animal->id;
// flash('Animal Added')->success();
return redirect()->route('animals.show')->with('animal', $animal);
}
我反而收到此错误。
Illuminate\Routing\Exceptions\UrlGenerationException thrown with message "Missing required parameters for [Route: animals.show] [URI: animals/{animal}]."
我也没有使用修复方法,我正在使用web.php文件中的资源
Route::resource('animals', 'AnimalsController');
这使我相信$ fillable不能按预期工作,而是使用上述方法,但是仍然无法工作。
如果我确实在模式中将Name设置为默认值,则必须为其他所有属性赋予默认值,而我不想这样做。
希望我已经提供了足够的信息供您使用,谢谢:D
答案 0 :(得分:0)
我发现了两个问题。
首先,我认为这不会起作用
$animal = Animal::create([
$request->input()
]);
这是一个包装在另一个数组内的数组。相反,让我们写:
$animal = Animal::create($request->input());
或者:
$animal = Animal::create($request->all());
在第二个示例中,我认为已创建了动物,但未正确指定重定向的路线:
return redirect()->route('animals.show')->with('animal', $animal);
让我们说:
return redirect()->route('animals.show', $animal->id);