问题
In the first time I want to save data to database from table this error appear.
ErrorException Undefined variable: value
so I have to manually input the data from tinker
or mysql
My Controller
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');
}
My Migration table
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();
});
}
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() !!}
the Model
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.
回答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
...
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.
回答2:
You have a Semicolon right after the foreach
loop definition: foreach ($id as $value);
and you are using the $value
in the next line $lastdevicetypeId = $value->id;
which is outside the scope of your loop.
You should remove the ;
following the the foreach
loop and change it to :
and add a endforeach;
where you want to end the loop.
Example:
foreach ($id as $value):
$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();
endforeach;
Or you can write this code as:
foreach ($id as $value){
$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();
}
来源:https://stackoverflow.com/questions/58387641/laravel-6-undefined-variable-value-only-error-in-the-first-time