【发布时间】:2019-05-19 04:12:31
【问题描述】:
我收到一个非常奇怪的“字段没有默认值”错误。有问题的字段是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。
-
您可以打印请求并添加到问题中吗?并在迁移时尝试此操作。
$table->decimal('starting_balance', 8, 2)->nullable(); -
为了防止此类问题,您应该使用 laravel 验证规则。
-
@akcoban 我已经添加了请求的打印,我也尝试了 nullable() 但它没有用
-
@Petar Vasilev 我无法弄清楚你的错误,我能建议的最后一件事是将这段代码:
$apartment -> starting_balance = $request -> starting_balance;放在:$apartment = new Apartment($request->except(['obekt_id', '_token']));之后
标签: php laravel laravel-5 eloquent