I am getting the following error when loading a page:
Undefined variable: userIds
However, the variavle is reaching the partial given when I try {{ isset($userIds) ? 'Yes' : 'No' }} it returns 'Yes' when the block of code is reached.
The error occurs with the variable $userIds in the following input type:
</li>
#foreach ($users as $index => $user)
<li class="list-group-item"
style="{{ (isset($usersFiltered) && is_array($usersFiltered) && !in_array($user->id, $usersFiltered) ) ?
'display: none;' : ''}}">
<div class="checkbox">
<label>
<input type="checkbox" class="awe-selectable" id="users[{{ $index }}]" name="users[{{ $index }}]"
value="{{ $user->id }}" {{ in_array($user->id,$userIds) ? 'checked' : '' }}>
<label for="users[{{ $index }}]">{{ $user->name }}</label>
</label>
</div>
</li>
#endforeach
My controller code sending the variable to the partial:
return view('core::admin.groups.partials._users_form', [
'group' => $group,
'users' => $users,
'usersFiltered' => $usersFiltered,
'userIds' => $userIds,
]);
Is it a syntax mistake? If I replace $userIds by any of the other variables, those too will return the same "undefined" error (f.e. with $usersFiltered)
Thanks in advance, will edit the post with more information as needed.
Solved!
The issue was that when the page was rendered, the variable was not defined in the main page render (/create), only in the method that rendered the partial, so when I was opening the main window, the variable was not yet defined because it was only received from the controller once a filter was applied.
Sollution:
/**
* GET: /admin/security/groups/create
*/
public function create()
{
**$userIds = !empty($request['userIds']) ? $request['userIds'] : [];**
(...)
return view('core::admin.groups.create', [
'group' => new Group,
**'userIds' => $userIds,**
'users' => $users,
'isCreating' => true,
]);
the syntax looks good to me, but I would suggest to you to do this:
// in your controller
foreach ($users as $user) {
$user->checked = false;
if ((is_array($userIds) and !empty($userIds)) and in_array($user->id, $userIds)) {
$user->checked = true;
}
}
now there is no need to send the $userIds to the view, since you fixed it in the controller.
// in your view
<input type="checkbox" class="awe-selectable" id="users[{{ $index }}]" name="users[{{ $index }}]" value="{{ $user->id }}" {{ ($user->checked) ? 'checked' : '' }}>
Happy coding!
Related
To satisfy the requirement (Add support for custom form fields without any plugin or library), I developed a module which allows admin to perform below actions.
Admin can create custom form fields.
Fields data should be encrypted when stored into database.
It can be mapped with multiple countries so if user belongs to that country, selected custom form fields will appear in profile.
Below is my code. Kindly suggest if there are any improvements needed.
Migration file to create custom form fields
public function up()
{
Schema::create('custom_forms', function (Blueprint $table) {
$table->id();
$table->string('title', 250);
$table->string('field_title', 250);
$table->string('field_name', 250);
$table->string('field_input_name', 250);
$table->enum(
'field_type',
[
'text',
'textarea',
'number',
'email',
'date',
'time',
'select',
'radio',
'checkbox',
'tel',
'url'
]
)->default('text');
$table->string('field_min_value', 10)->nullable();
$table->string('field_max_value', 10)->nullable();
$table->text('field_option_values')->nullable();
$table->string('field_validation_pattern', 250)->nullable();
$table->string('field_validation_message', 250)->nullable();
$table->string('field_placeholder', 250)->nullable();
$table->tinyInteger('is_required')->default(0);
$table->integer('country_id');
$table->tinyInteger('is_active')->default(1);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('custom_forms');
}
Migration file to store custom form field values
public function up()
{
Schema::create('custom_form_values', function (Blueprint $table) {
$table->id();
$table->foreignId('form_id')->references('id')->on('custom_forms');
$table->foreignId('user_id')->references('id')->on('users');
$table->mediumText('value')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('custom_form_values');
}
White creating a custom form field, admin can select multiple countries for single. So, I've created a loop for all selected countries and stored the data.
CustomFormController.php
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
foreach ($request->country_id as $country) {
$custom_form = new CustomForm();
$custom_form->title = $request->name;
$custom_form->field_title = $request->name . '-' . Str::random(10);
$custom_form->field_name = $request->field_name;
$custom_form->field_input_name = preg_replace('/\s+/', '', $request->name) . '-' . Str::random(10);
$custom_form->field_type = $request->field_type;
$custom_form->field_min_value = $request->field_min_value;
$custom_form->field_max_value = $request->field_max_value;
$custom_form->field_option_values = $request->field_option_values;
$custom_form->field_validation_pattern = $request->field_validation_pattern;
$custom_form->field_validation_message = $request->field_validation_message;
$custom_form->field_placeholder = $request->field_placeholder;
$custom_form->is_required = $request->field_is_required;
$custom_form->country_id = $country;
$custom_form->save();
}
return redirect()->route('admin.custom-forms.index');
}
In CustomForm.php model, added relation to the values to get data for selected custom form field.
public function values()
{
return $this->belongsTo(CustomFormValue::class, 'id', 'form_id')->select(['id', 'value','form_id']);
}
In CustomFormValue.php model, defined encrypted field to match requirement no. 2.
protected $casts = [
'value' => 'encrypted',
];
Below code is from edit_profile.blade.php to display all custom form fields.
#forelse ($custom_forms as $form)
<div class="form-group">
<label class="required"
for="{{ $form->field_input_name }}">{{ $form->field_name }}</label>
{{-- FOr Text area --}}
#if ($form->field_type === 'textarea')
<textarea class="form-control" type="{{ $form->field_type }}"
name="custom_form_{{ $form->id }}" id="{{ $form->field_input_name }}"
minlength="{{ $form->field_min_value }}"
maxlength="{{ $form->field_max_value }}"
#if($form->field_validation_pattern) pattern="{{ $form->field_validation_pattern }}" #endif
title="{{ $form->field_validation_message }}"
placeholder="{{ $form->field_placeholder }}"
{{ $form->is_required == 1 ? 'required' : '' }}>{{ $form->values ? $form->values->value : null }}</textarea>
<span class="help-block">{{ $form->field_placeholder }}</span>
{{-- for select option --}}
#elseif ($form->field_type === 'select')
#php
$option_values = explode(',', $form->field_option_values);
$is_selected = $form->values ? $form->values->value : '';
#endphp
<div class="form-group">
<select class="form-control" type="{{ $form->field_type }}"
name="custom_form_{{ $form->id }}"
id="{{ $form->field_input_name }}"
#if($form->field_validation_pattern) pattern="{{ $form->field_validation_pattern }}" #endif
title="{{ $form->field_validation_message }}"
placeholder="{{ $form->field_placeholder }}"
{{ $form->is_required == 1 ? 'required' : '' }}>
#forelse ($option_values as $value)
<option #if ($value == $is_selected) selected #endif value="{{ $value }}">
{{ $value }}</option>
#empty
#endforelse
</select>
<span class="help-block">{{ $form->field_placeholder }}</span>
</div>
{{-- for radio buttons --}}
#elseif ($form->field_type === 'radio')
#php
$option_values = explode(',', $form->field_option_values);
$is_selected = $form->values ? $form->values->value : '';
#endphp
<div class="col-sm-10">
#forelse ($option_values as $value)
<div>
<label>
<input class=" " type="{{ $form->field_type }}"
name="custom_form_{{ $form->id }}"
id="{{ $form->field_input_name }}"
#if($form->field_validation_pattern) pattern="{{ $form->field_validation_pattern }}" #endif
title="{{ $form->field_validation_message }}"
placeholder="{{ $form->field_placeholder }}"
value="{{ $value }}" #if ($value == $is_selected) checked #endif
{{ $form->is_required == 1 ? 'required' : '' }}>{{ $value }}
</label>
</div>
#empty
#endforelse
</div>
{{-- for checkbox --}}
#elseif ($form->field_type === 'checkbox')
#php
$option_values = explode(',', $form->field_option_values);
$is_selected = $form->values ? $form->values->value : '';
#endphp
<div class="col-sm-10">
#forelse ($option_values as $value)
<div>
<label>
<input class=" " type="{{ $form->field_type }}"
name="custom_form_{{ $form->id }}[]"
id="{{ $form->field_input_name }}"
#if($form->field_validation_pattern) pattern="{{ $form->field_validation_pattern }}" #endif
title="{{ $form->field_validation_message }}"
placeholder="{{ $form->field_placeholder }}"
value="{{ $value }}" #if (str_contains($is_selected,$value)) checked #endif
>{{ $value }}
</label>
</div>
#empty
#endforelse
</div>
#else
<input class="form-control " type="{{ $form->field_type }}"
name="custom_form_{{ $form->id }}" id="{{ $form->field_input_name }}"
minlength="{{ $form->field_min_value }}"
maxlength="{{ $form->field_max_value }}" {{-- pattern="{{ $form->field_validation_pattern }}" --}}
title="{{ $form->field_validation_message }}"
placeholder="{{ $form->field_placeholder }}"
value="{{ $form->values ? $form->values->value : null }}"
#if($form->field_validation_pattern) pattern="{{ $form->field_validation_pattern }}" #endif
{{ $form->is_required == 1 ? 'required' : '' }}>
<span class="help-block">{{ $form->field_placeholder }}</span>
#endif
</div>
#empty
{{-- Message to display if there is no custom field added--}}
#endforelse
To store user entered custom form field values, in UserController.php
foreach ($custom_values as $key => $value) {
$form_value = Str::contains($key, 'custom_form_');
if ($form_value == 1) {
$form_id = str_replace('custom_form_', '', $key);
$custom_form_value = CustomFormValue::updateOrCreate([
'form_id' => $form_id,
'user_id' => Auth::user()->id
], [
'form_id' => $form_id,
'user_id' => Auth::user()->id,
'value' => is_array($value) ? implode(', ', $value) : $value
]);
}
}
One user can have many custom form field values. So, in User.php model,
public function customFormValue()
{
return $this->hasMany(CustomFormValue::class, 'user_id');
}
And finally, to display custom form values, in profile.blade.php
#forelse ($custom_forms as $form)
<tr>
<td>
{{ $form->field_name }}: <strong class="pull-right">{{ $form->values ? $form->values->value : '' }}</strong>
</td>
</tr>
#empty
{{-- Message if any --}}
#endforelse
So, this is what I have done so far to add support for custom form field without any plugin or library. Let me know if there is any room for improvement.
I hope it may help others who are looking for something similar.
Here are some points that you can implement for a better scalable system.
1) custom_forms table
We can store the nullable fields in JSON. So we can avoid the multiple nullable fields because it can be possible to have 50% nullable values. It is effective for storage purposes and will make a more efficient and scalable DB design.
2) custom_form_values table
$table->mediumText('value')->nullable() TO $table->json('value')->nullable()
NOTE - The JSON column type is recommended as it allows you to do SQL queries on JSON data. Here is a quick doc link https://dev.mysql.com/doc/refman/8.0/en/json.html
3) CustomFormController.php
Here we can do the bulk insertion. So the query will not fire every time. Reference - https://stackoverflow.com/a/29723968/7796116
4) edit_profile.blade.php
AVOID USING MULTIPLE IF/ELSE. IT WILL CREATES ISSUES AT TIME OF SCALIBILITY. Use components instead so it will make code more readable and scalable.
Here is the laravel components doc https://laravel.com/docs/9.x/blade#components
5) General Tips
Try to use the Null Coalescing operator instead ternary.
Hope this will help you.
Controller article
public function update(Request $request, Article $article){
$article->update($request->except('slug', 'image_path'));
if ($request->hasFile('image_path')) {
$image = $request->file('image_path');
$new_name = rand() . '.' . $image->getClientOriginalExtension();
$image->move(public_path('images'), $new_name);
$article->image_path = $new_name;
$article->save();
}
$article->categories()->detach();
if ($request->has('categories')) {
$article->categories()->attach($request->input('categories'));
}
$user=auth()->user();
return redirect()->back()->with('message', 'Raksts atjaunots!');
}
public function edit(Article $article){
$user=auth()->user();
return view('edit',[
'article' => $article,
'categories' => Category::with('children')->where('parent_id',0)->get(),
'delimiter' => ''
]);
}
edit blade
<form class="form-horizontal" action="{{route('update', $article)}}" method="post" enctype="multipart/form-data" style="display: flex;justify-content: center;flex-direction: column;">
<input type="hidden" name="_method" value="put">
{{ csrf_field() }}
{{-- Form include --}}
<img src="{{URL::to('/images').'/'.$article->image_path}}" alt="">
#include('partials.form')
<input type="hidden" name="modified_by" value="{{Auth::id()}}">
</form>
link to the form
<tbody>
#foreach ($articles_suggest_user as $article)
<a class="btn btn-default" href="{{route('edit', $article)}}"><i class="fa fa-edit"></i></a>
<?php } ?>
#endforeach
</tbody>
web.php
Route::get('/home/edit/{id}','Controller_Article_parents#edit', function () {
return view('/home/edit');
})->name('edit');
Route::get('/home/update/','Controller_Article_parents#update', function () {
return view('/home');
})->name('update');
When i clicked the link, i move to for example this url http://127.0.0.1:8000/home/edit/190
But my form is empty... input empty. How I can do when I open form it's display me input information ?
When click my link its display me form, but form is empty.
form example
<div class="rtq">
<label for="parent_id">Cat</label>
<span class="required">*</span>
<select
id="parent_id"
class="form-control"
name="categories[]"
multiple=""
required
>
#include('admin.categories.partials.categories',
['categories' => $categories,
'current' => $article,
'delimiter' => $delimiter])
</select>
</div>
<div class="rtq">
<label for="">Descript</label>
<textarea
name="description_short"
class="form-control"
id="description_short"
>
{{ $article->description_short ?? "" }}
</textarea>
</div>
It my form . Mini example
If you want to redirect with form input, you can use the withInput() function
return back()->withInput();
https://laravel.com/docs/7.x/redirects#creating-redirects
Now, this function is designed to be used with form validation and may not be what you want.
What I have done in the past when combining create & edit views into a single template is something like this:
{!! Form::text('name', isset($model) ? $model->name : null, [
'class' => 'form-control',
'placeholder' => 'Please fill out this field &hellips;',
'required' => true
]) !!}
This uses the Laravel Collective HTML library. However the principle is the same if you are using raw hmtl like so:
<input type="text" value="{{ isset($model) ? $model->name : null }}">
Round 2 Electric Boogaloo
You aren't returning a variable called $article to your edit.blade.php.
Round 3 Apple Tree
Originally, you appeared to be calling both a controller action AND also a callback function. Stick to the controller action only like so:
Route::get('/home/edit/{id}','Controller_Article_parents#edit')->name('edit');
Then within your edit() function on the Controller you will want to do this:
public function edit ($id) {
return view('/home/edit', [
'article' => Article::find($id)
]);
}
I am trying to store my checkbox values in database. I have these three tables.
fields
id name
1 gender
2 looking_for
field_values (here field_id references id on fields table)
id field_id value label
1 1 1 Men
2 1 2 Women
3 2 3 Relationship
4 2 4 Friendship
5 2 5 Marriage
user_interests (here field_id references field_id on field_values table and value_id references value on field_values table)
user_id field_id value_id
1 1 2
1 2 4
1 2 5
gender in blade uses option values and looking_for uses checkbox values. I made one function that is trying to update both of them. I use two foreaches in my function and I am able to successfully update gender option, but I am unable to update looking_for option. When I click submit button nothing happens, also when I dump anything inside that foreach that is supposed to update checkboxes it doesn't dump. Any help is greatly appreciated. Here is my code.
web.php
Route::patch('profile/interests', 'UserProfileController#updateInterestsData')->name('profile.update.interests.data');
UserProfileController.php
public function updateInterestsData(UpdateInterestsDataRequest $request)
{
$user = User::with('userProfile')->where('id', Auth::user()->id)->firstOrFail();
$request->validated();
$userId = $request->input('user_id') ? $request->input('user_id') : Auth::user()->id;
$data = $request->all();
$options = [
'gender' => 1,
'looking_for' => 2
];
foreach ($options as $fieldName => $fieldId) {
if (! empty($data[$fieldName])) {
DB::table('user_interests')
->where('user_id', $userId)
->where('field_id', $fieldId)
->delete();
if (is_array($data[$fieldName])) { // CHECKBOX FIELDS AND HERE IT DOESN'T WORK!!!
//dd('DIE!!!!!!') IT DOESN'T ENTER HERE!!!
foreach ($data[$fieldName] as $key => $value) {
DB::table('user_interests')->insert([
'user_id' => $userId,
'field_id' => $fieldId,
'value_id' => $value
]);
}
} else { // SELECT FIELDS!!!
DB::table('user_interests')->insert([
'user_id' => $userId,
'field_id' => $fieldId,
'value_id' => $data[$fieldName]
]);
}
}
}
$user->userProfile->update(
[
'age_from_preference' => $request->age_from_preference,
'age_to_preference' => $request->age_to_preference,
'updated_at' => Carbon::now()
]
);
$request->user()->save();
return redirect()->route('profile.show', [$user->username]);
}
index.blade.php
<form action="{{ route('profile.update.interests.data') }}" method="POST" class="flex">
#method('PATCH')
#csrf
<div class="form-group">
<span>Interessiert an</span>
{{-- wrong value - selected --}}
#isset($options)
#foreach($options as $name => $fieldData)
#if ($name == 'gender')
<div class="selectHolder">
<select name="{{ $name }}">
<option selected="true" disabled="disabled" value="" style="display:none">bitte auswählen</option>
#foreach($fieldData['data'] as $value => $label)
<option value="{{ $value }}" {{ isset($data[$fieldData['label']['id']]) ? (in_array($value, $data[$fieldData['label']['id']]) ? 'selected' : '') : '' }}>
{{ $label }}
</option>
#endforeach
</select>
</div>
<?php
unset($options[$name]);
?>
#endif
#endforeach
#endisset
</div>
<div class="form-group">
<span>Im Alter von</span>
<input type="text" placeholder="XX" maxlength="2" value="{{ $userForShowProfile->userProfile->age_from_preference ?? "" }}" name="age_from_preference">
<span>Jahren bis</span>
<input type="text" placeholder="XX" maxlength="2" value="{{ $userForShowProfile->userProfile->age_to_preference ?? "" }}" name="age_to_preference">
<span>Jahren</span>
</div>
{{-- wrong value - checked --}}
#isset($options)
<div class="form-group flex mt-5">
#foreach($options as $name => $fieldData)
#if ($name == 'looking_for')
#foreach ($options[$name]['data'] as $value=>$label)
<div class="interestedIn">
<input type="checkbox" name="{{ $name.'-'.$value }}" value="{{ $value }}" {{ isset($data[$fieldData['label']['id']]) ? (in_array($value, $data[$fieldData['label']['id']]) ? 'checked' : null) : '' }}>
<label for="{{$name}}-{{ $value }}">{{ $label }}</label>
</div>
#endforeach
#endif
#endforeach
</div>
#endisset
<div class="form-group">
<label for="" class="textBold">Button</label>
<input type="submit" class="form-control" name="submit" value="BUTTON">
</div>
</form>
code for $options variable
public static function getProfileLookingForDisplayOptions()
{
$options = [
'gender' => ['id' => 1, 'label' => "Interessiert an"],
'looking_for' => ['id' => 2, 'label' => ""]
];
$data_options = [];
foreach ($options as $field => $value) {
$data_options[$field]['data'] = Value::fieldValues($field);
$data_options[$field]['label'] = $options[$field];
if (!in_array($field, ['gender', 'looking_for'])) {
$data_options[$field]['data'][100] = "Doesn't matter";
}
}
//dd($data_options);
return $data_options;
}
If I understand your problem correctly, to deal with multiple checkboxes on PHP you need to add [] to its name property. That way, PHP knows it should interpret theses values as an array.
Also, your input name is not matching with $data[$fieldName].
Do it like this:
#isset($options)
<div class="form-group flex mt-5">
#foreach($options as $name => $fieldData)
#if ($name == 'looking_for')
#foreach ($options[$name]['data'] as $value=>$label)
<div class="interestedIn">
<input type="checkbox" name="{{ $name }}[]" value="{{ $value }}" {{ isset($data[$fieldData['label']['id']]) ? (in_array($value, $data[$fieldData['label']['id']]) ? 'checked' : null) : '' }} id="{{$name}}-{{ $value }}">
<label for="{{$name}}-{{ $value }}">{{ $label }}</label>
</div>
#endforeach
#endif
#endforeach
</div>
#endisset
It's my first time using blade and I'm a little confused with this. I need to show a form like what it comes with the default edit-add view in Voyager, but I need to display some inputs based on roles. I am filtering by the current user that is logged in and that is working fine, but I need to hide/display some inputs of the form based on that.
How can I accomplish this in the blade view?
Here is how Voyager take the data in the blade view:
#php
$dataTypeRows = $dataType->{(isset($dataTypeContent->id) ? 'editRows' :'addRows' )};
#endphp
#foreach($dataTypeRows as $row)
<!-- GET THE DISPLAY OPTIONS -->
#php
$options = json_decode($row->details);
$display_options = isset($options->display) ? $options->display : NULL;
#endphp
#if ($options && isset($options->formfields_custom))
#include('voyager::formfields.custom.' . $options->formfields_custom)
#else
<div class="form-group #if($row->type == 'hidden') hidden #endif #if(isset($display_options->width)){{ 'col-md-' . $display_options->width }}#else{{ '' }}#endif" #if(isset($display_options->id)){{ "id=$display_options->id" }}#endif>
{{ $row->slugify }}
<label for="name">{{ $row->display_name }}</label>
#include('voyager::multilingual.input-hidden-bread-edit-add')
#if($row->type == 'relationship')
#include('voyager::formfields.relationship')
#else
{!! app('voyager')->formField($row, $dataType, $dataTypeContent) !!}
#endif
#foreach (app('voyager')->afterFormFields($row, $dataType, $dataTypeContent) as $after)
{!! $after->handle($row, $dataType, $dataTypeContent) !!}
#endforeach
</div>
#endif
#endforeach
If my table 'example' contains: id, name, address and phone. How do I display in this view form only inputs for name and address?
Thanks in advance.
You should try this:
public function edit(Request $request, $id)
{
$user = Example::find($id);
return view(‘viewname’,compact(‘user’));
}
edit-add.blade.php
<input type = “text” name=”name” value=”#if(isset($user->name)){{ old('name', $user->name) }}#else{{old('name')}}#endif”>
<input type = “text” name=”name” value=”#if(isset($user->address)){{ old('address', $user->address) }}#else{{old('address')}}#endif”>
i cant understand what do you mean but
for showing a value in input in a view first you should send the value to view for example in controller you have :
public function test()
{
.....
return view('myView',compact('user'));
}
then in your view :
<input type="text" value"{{$user->address}}">
<input type="text" value"{{$user->first_name}}">
You can edit it from ADMIN interface without using any code
Just go to the settings menu and click BREAD
I have the following controller, which sends data to a Laravel Blade view:
Controller:
public function create()
{
$schools = School::all()->sortBy('school_type');
return view('invoices.create')->with([
'schools' => $schools,
'dayTypes' => $dayTypes,
]);
}
In that Laravel blade view there is a form:
<form method="GET" action="{{ route('invoices.choose-periods') }}">
<div class="form-group {{ $errors->has('school') ? 'has-error' : '' }}">
<label>School</label>
<select id="school" class="form-control" name="school[]" multiple size="{{ $schools->count() }}" required>
#foreach ($schools as $school)
<option value="{{ $school->id }}">{{ $school->name }}</option>
#endforeach
</select>
#if ($errors->has('school'))
<span class="help-block">
<strong>{{ $errors->first('school') }}</strong>
</span>
#endif
</div>
<button type="submit" class="btn btn-success btn-sm pull-right">Submit</button>
</form>
As you can see from the HTML, the form is a multi-select form, with the resulting data stored in a school[] array.
On submission of the form, I do a test die and dump on request('school') and see that for every option I have selected, the value seems to have been logged twice. For example, choosing only one option gives me:
array:2 [▼
0 => "15"
1 => "15"
]
Any ideas? Thanks!
I have only worked on laravel 5.7. Try this It is working for me.
Since you are passing 2 objects
return view('invoices.create')->with([
'schools' => $schools,
'dayTypes' => $dayTypes,
]);
It is obvious you will get 2 errors.
In your controller change this
public function create()
{
$schools = School::all()->sortBy('school_type');
return view('invoices.create')->with([
'schools' => $schools,
'dayTypes' => $dayTypes,
]);
}
to this
public function create(){
$schools = School::all()->sortBy('school_type');
return view('invoices.create', ['schools' => $schools]),
]);
}