Laravel 6-``未定义变量:值''仅在第一次出现错误 [英] Laravel 6 - `Undefined variable: value` only error in the first time
问题描述
第一次将数据从表保存到数据库时,会出现此错误.
In the first time I want to save data to database from table this error appear.
ErrorException 未定义的变量:值
ErrorException Undefined variable: value
所以我必须手动输入tinker
或mysql
so I have to manually input the data from tinker
or mysql
我的控制器
public function store(Request $request)
{
$validateData = $request->validate([
'name_device_type' => 'required|max:255',
'signature' => 'Nullable'
]);
$id = DeviceType::getidDeviceTypes();
foreach ($id as $value); // Error happend in this line.
$lastdevicetypeId = $value->id;
$newdevicetypeId = $lastdevicetypeId + 1;
$GetnewdevicetypeId = sprintf('DT%04d', $newdevicetypeId);
$devicetypes = new DeviceType();
$devicetypes->idDeviceType = $GetnewdevicetypeId;
$devicetypes->name_device_type = $request->input('name_device_type');
$devicetypes->signature = $request->input('signature');
$devicetypes->save();
return redirect('/devicetypes')->with('success', 'New Device Type is added');
}
我的迁移表
public function up()
{
Schema::create('device_types', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('idDeviceType');
$table->string('name_device_type');
$table->mediumText('signature');
$table->timestamps();
});
}
我的create.blade.php
My create.blade.php
{!! Form::open(['action' => 'DeviceTypesController@store', 'method' => 'POST']) !!}
<div class="form-group">
{!! Form::label('name_device_type', 'Type Device'); !!}
{!! Form::text('name_device_type', '', ['class' => 'form-control', 'placeholder' => 'Type Device']); !!}
</div>
<div class="form-group">
{!! Form::label('signature', 'Signature (Optional)'); !!}
{!! Form::textarea('signature', '', ['id' => 'classic-ckeditor5', 'class' => 'form-control', 'placeholder' => 'Signature']); !!}
</div>
{{ Form::button('<i class="far fa-save"></i> Submit', ['type' => 'submit', 'class' => 'btn btn-info'] ) }}
{!! Form::close() !!}
模型
class DeviceType extends Model
{
// Table Name
protected $table = 'device_types';
// Primary Key
protected $primaryKey = 'idDeviceTypes';
// Timestamps
public $timestamps = true;
public $incrementing = false;
public static function getidDeviceType(){
return $getidDeviceType = DB::table('device_types')->orderBy('id','desc')->take(1)->get();
}
}
但是如果表中有数据,此错误就会消失,而且如果我删除所有数据并使表为空,也会再次出现此错误.
But if the table has data this error disappear, also this error will appear again if I remove every data and made the table empty.
推荐答案
您可以获取表的最大'id'并将其添加1,而不必获取整个记录:
You could grab the max 'id' of the table and add 1 to it without having to grab a whole record:
...
// validation
$newdevicetypeId = DeviceType::max('id') + 1;
$GetnewdevicetypeId = sprintf('DT%04d', $newdevicetypeId);
// new DeviceType
...
可以选择创建一个模型事件,该事件可以在创建记录后设置此特定字段,因此它可以使用自己的ID.
There is the option of having a model event that can set this particular field after the record has been created so it has its own id to use.
这篇关于Laravel 6-``未定义变量:值''仅在第一次出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!