How to transfer data to a view?
Vasiliy-Makogon opened this issue · 2 comments
Vasiliy-Makogon commented
Hey. At the first step there is a form with a select list, you need to transfer data from the database to this list. In the standard action, we will do this:
public function create(Request $request)
{
return view('backend.docflow.document.create')
->withDocumentTypes(DocflowHelper::documentTypeTreeToSelectArray(DocumentType::getLevel()))
->withDocumentStatuses(DocumentStatus::all()->where('active', '=', '1')->sortBy('id'))
->withUsers(User::all()->sortBy('last_name, first_name'));
}
How to do it for a step?
ycs77 commented
Because each step is injected into the view of the step, so just add the method to return the data in the step class.
For example:
The
getOptions
method is custom, can be changed at will.
app/Steps/User/NameStep.php
<?php
...
class NameStep extends Step
{
...
public function getOptions()
{
return [
'Taylor',
'Lucas',
];
}
}
resources/views/steps/user/name.blade.php
<div class="form-group">
<label for="name">Select name</label>
<select id="name" name="name" class="form-control{{ $errors->has('name') ? ' is-invalid' : '' }}">
<option value="">Select...</option>
@foreach ($step->getOptions() as $option)
<option value="{{ $option }}" @if (old('name') ?? $step->data('name') === $option) @endif>{{ $option }}</option>
@endforeach
</select>
@if ($errors->has('name'))
<span class="invalid-feedback">{{ $errors->first('name') }}</span>
@endif
</div>
Use your data for example:
app/Steps/Docflow/FirstStep.php
<?php
...
class FirstStep extends Step
{
...
public function getDocumentTypes()
{
return DocflowHelper::documentTypeTreeToSelectArray(DocumentType::getLevel());
}
public function getDocumentStatuses()
{
return DocumentStatus::all()->where('active', '=', '1')->sortBy('id');
}
public function getUsers()
{
return User::all()->sortBy('last_name, first_name');
}
}
resources/views/steps/docflow/first.blade.php
{{ $step->getDocumentTypes() }}
{{ $step->getDocumentStatuses() }}
{{ $step->getUsers() }}
Happy coding 😄 !
Vasiliy-Makogon commented
Because each step is injected into the view of the step, so just add the method to return the data in the step class.
Great, thank you!