我试图根据最后一个员工编号自动自动生成一个顺序的员工号码。这是我在模型,控制器和视图中所拥有的,它没有显示任何内容。
当加载初始创建页面时,员工编号的生成应该发生。
请让我知道如何解决此问题,因为我没有遇到任何错误。
模型
public function getNextEmployeeNumber()
{
$lastEmployee = Employee::orderBy('created_at', 'desc')->first();
if ( ! $lastEmployee )
$number = 0;
else
$number = substr($lastEmployee->order_id, 3);
return 'EMP' . sprintf('%06d', intval($number) + 1);
}
控制器
public function create()
{
if (! Gate::allows('employee_create')) {
return abort(401);
}
$employee_number = new Employee();
$employee_number->getNextEmployeeNumber();
$services = AppService::get()->pluck('name', 'id')->prepend(trans('global.app_please_select'), '');
$cities = AppCity::get()->pluck('city', 'id')->prepend(trans('global.app_please_select'), '');
$states = AppState::get()->pluck('state', 'id')->prepend(trans('global.app_please_select'), '');
$created_bies = AppUser::get()->pluck('name', 'id')->prepend(trans('global.app_please_select'), '');
$emergency_contacts = AppEmergencyContact::get()->pluck('name', 'id')->prepend(trans('global.app_please_select'), '');
return view('admin.employees.create', compact('services', 'cities', 'states', 'created_bies', 'emergency_contacts', 'employee_number' ));
}
查看
@section('content')
<h3 class="page-title">@lang('global.employee.title')</h3>
{!! Form::open(['method' => 'POST', 'route' => ['admin.employees.store']]) !!}
<div class="panel panel-default">
<div class="panel-heading">
@lang('global.app_create')
</div>
<div class="panel-body">
<div class="row">
<div class="col-xs-12 form-group">
{!! Form::label('first_name', trans('global.employee.fields.first-name').'*', ['class' => 'control-label']) !!}
{!! Form::text('first_name', old('first_name'), ['class' => 'form-control', 'placeholder' => '', 'required' => '']) !!}
<p class="help-block"></p>
@if($errors->has('first_name'))
<p class="help-block">
{{ $errors->first('first_name') }}
</p>
@endif
</div>
</div>
<div class="row">
<div class="col-xs-12 form-group">
{!! Form::label('middle_name', trans('global.employee.fields.middle-name').'', ['class' => 'control-label']) !!}
{!! Form::text('middle_name', old('middle_name'), ['class' => 'form-control', 'placeholder' => '']) !!}
<p class="help-block"></p>
@if($errors->has('middle_name'))
<p class="help-block">
{{ $errors->first('middle_name') }}
</p>
@endif
</div>
</div>
<div class="row">
<div class="col-xs-12 form-group">
{!! Form::label('last_name', trans('global.employee.fields.last-name').'*', ['class' => 'control-label']) !!}
{!! Form::text('last_name', old('last_name'), ['class' => 'form-control', 'placeholder' => '', 'required' => '']) !!}
<p class="help-block"></p>
@if($errors->has('last_name'))
<p class="help-block">
{{ $errors->first('last_name') }}
</p>
@endif
</div>
</div>
<div class="row">
<div class="col-xs-12 form-group">
{!! Form::label('employee_number', trans('global.employee.fields.employee-number').'*', ['class' => 'control-label']) !!}
{!! Form::number('employee_number', old('employee_number'), ['class' => 'form-control', 'placeholder' => '', 'required' => '']) !!}
<p class="help-block"></p>
@if($errors->has('employee_number'))
<p class="help-block">
{{ $errors->first('employee_number') }}
</p>
@endif
</div>
</div>
您可能在create()
方法中正确创建它:
$employee_number = new Employee();
$employee_number->getNextEmployeeNumber();
因此,此时$employee_number
可能在该方法中具有正确的值。
然后,您将其传递到视图中,作为在同一create()
方法中的变量$employee_number
,此处:
return view('admin.employees.create', compact('services', 'cities', 'states', 'created_bies', 'emergency_contacts', '**employee_number**' ));
但是,我看不到您在trans之外的刀片视图中引用变量 $employee_number
的地方。如果您使用VAR,这将使您了解发生了什么。即,我看到您看一个以前的表单值,我看到您看一个错误袋,但我看不到您在视图中称呼该变量的位置。这将否定显示值。因此 - 您可能的过程可能正确,我认为您只是忘了在变量中添加一个呼叫。请记住,您实际上还没有创建员工,您有一个新对象,但是据我所知,此时仅附加了雇员。