我正在尝试以编程方式自动根据上一个员工编号自动生成一个连续的员工编号。这是我在“模型”,“控制器”和“视图”中拥有的东西,它什么也不显示。
加载初始创建页面时应生成员工编号。
请让我知道如何解决此问题,因为我没有任何错误。
型号
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 = \App\Service::get()->pluck('name', 'id')->prepend(trans('global.app_please_select'), '');
$cities = \App\City::get()->pluck('city', 'id')->prepend(trans('global.app_please_select'), '');
$states = \App\State::get()->pluck('state', 'id')->prepend(trans('global.app_please_select'), '');
$created_bies = \App\User::get()->pluck('name', 'id')->prepend(trans('global.app_please_select'), '');
$emergency_contacts = \App\EmergencyContact::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>
答案 0 :(得分:0)
您可能在此处的a
方法中正确创建了它:
create()
因此$employee_number = new Employee();
$employee_number->getNextEmployeeNumber();
在这一点上可能在方法中具有正确的值。
然后您将其作为变量$employee_number
通过相同的$employee_number
方法传递到视图中:
create()
但是,在trans之外的刀片视图中,我看不到您指的是变量 return view('admin.employees.create', compact('services', 'cities', 'states', 'created_bies', 'emergency_contacts', '**employee_number**' ));
的任何地方。如果您使用var代替,这将使您对发生的事情有所了解。 IE浏览器,我看到您查看的是先前的表单值,并且看到了错误包,但看不到您在视图中调用变量的位置。这将否定显示值。所以-您可能已使过程正确,我想您只是忘记了对变量添加调用。记住,您实际上还没有创建一个雇员,您有一个新对象,但是据我所知,此时只有employee_number字段附加到该对象。