我收到一个非常奇怪的“字段没有默认值”错误。有问题的字段是starting_balance
。看来Laravel试图将starting_balance
字段保存为数据库,好像它是空的,但我确定不是。任何帮助表示赞赏。
我尝试为迁移中的字段添加默认值,但这仍然无济于事。
这是我对该模型的迁移:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateApartmentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('apartments', function (Blueprint $table) {
$table->increments('id');
$table->integer('entrance_id');
$table->integer('user_id')->nullable()->default(null);
$table->integer('floor');
$table->integer('apt_number');
$table->string('square_meters', 64)->nullable();
$table->decimal('percent_ideal_parts', 10, 3)->nullable();
$table->decimal('starting_balance', 8, 2);
$table->string('animals', 200)->nullable();
$table->string('other_information', 2048)->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('apartments');
}
}
以下是控制器处理请求的相关部分:
public function store(Request $request, ApartmentRequest $apartmentRequest)
{
$apartment = new Apartment($request->except(['obekt_id', '_token']));
if ($apartment->save())
{
Session::flash('success', 'Апартамента беше запазен успешно.');
return redirect('/entrances/' . $apartment->entrance->id . '/apartments');
}
else
{
Session::flash('error', 'Имаше проблем докато запазвахме апартамента.');
return back();
}
}
这是我的模特:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Apartment extends Model
{
protected $fillable = ['entrance_id', 'user_id', 'floor', 'apt_number', 'square_meters', 'percent_ideal_parts', 'starting_balance', 'animals', 'other_information'];
public function people()
{
return $this->hasMany('App\Person');
}
public function entrance()
{
return $this->belongsTo('App\Entrance');
}
public function taksiDomoupravitel()
{
return $this->hasMany('App\TaksaDomoupravitel');
}
public function taksiFro()
{
return $this->hasMany('App\TaksaFro');
}
public function payments()
{
return $this->hasMany('App\Payment');
}
public function user()
{
return $this->belongsTo('App\User');
}
}
这是$ request-> except('obekt_id','_token')部分的dd:
array:9 [▼
"entrance_id" => "1"
"user_id" => null
"floor" => "15"
"apt_number" => "15"
"square_meters" => null
"percent_ideal_parts" => "15"
"starting_balance" => "-20"
"animals" => null
"other_information" => null
]
这是我的ApartmentRequest验证类:
<?php
namespace App\Http\Requests;
use App\Rules\UniqueFloorAndAptNumber;
use Illuminate\Foundation\Http\FormRequest;
class ApartmentRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'obekt_id' => 'required|exists:obekti,id',
'entrance_id' => 'required|exists:entrances,id',
'user_id' => 'exists:users,id|nullable',
'floor' => ['required', 'numeric', new UniqueFloorAndAptNumber],
'apt_number' => ['required', 'numeric', new UniqueFloorAndAptNumber],
'percent_ideal_parts' => 'required|numeric',
'starting_balance' => 'required|numeric',
'animals' => 'max:200',
'other_information' => 'max:2048',
];
}
}
这是起始余额字段的刀片视图部分:
<div class="form-group{{ $errors->has('starting_balance') ? ' has-error' : '' }}">
<label for="starting_balance" class="col-md-4 control-label red-text">Моля въведете начално салдо *</label>
<div class="col-md-6">
<input id="starting_balance" type="text" class="form-control" name="starting_balance" value="{{ old('starting_balance') }}">
@if ($errors->has('starting_balance'))
<span class="help-block">
<strong>{{ $errors->first('starting_balance') }}</strong>
</span>
@endif
</div>
</div>
答案 0 :(得分:0)
错误非常明显:fab: {
margin: theme.spacing.unit,
width: '100px',
fontFamily: theme.typography.fontFamily[1],
fontSize: theme.typography.fontSize.buttonFont,
textTransform: 'none',
},
primaryLight: {
backgroundColor: 'white',
color: 'black',
'&:hover': {
backgroundColor: theme.palette.primary.light,
color: 'white'
}
},
没有默认值。
您可以通过添加默认值来更新迁移文件,如下所示:
starting_balance
答案 1 :(得分:0)
好的,我有你的问题, 您验证ApartmentRequest $ apartmentRequest但使用Request $ request 您的代码应该像这样
public function store(ApartmentRequest $apartmentRequest)
{
$apartment = new Apartment($apartmentRequest->except(['obekt_id', '_token']));
if ($apartment->save())
{
Session::flash('success', 'Апартамента беше запазен успешно.');
return redirect('/entrances/' . $apartment->entrance->id . '/apartments');
}
else
{
Session::flash('error', 'Имаше проблем докато запазвахме апартамента.');
return back();
}
}
答案 2 :(得分:0)
尝试一下:
$apartment -> starting_balance = $request -> starting_balance;
之后:
$apartment = new Apartment($request->except(['obekt_id', '_token']));