Я получаю очень странную ошибку "Поле не имеет значения по умолчанию".Поле, о котором идет речь, 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-> кроме ('obekt_id', '_token') часть:
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>