x-larastrap::select is the equivalent of a "select" HTML node, with his own options .
@php
$options = ['red' => 'Red', 'green' => 'Green', 'blue' => 'Blue'];
@endphp
<x-larastrap::select label="Choose One" :options="$options" />
<div class="row mb-3">
<label for="select-d804a733484fcd4cdf5f105efbca9035" class="col-4 col-form-label">Choose One</label>
<div class="col-8">
<select class="form-select" name="lGlrQYspQG">
<option value="red">
Red
</option>
<option value="green">
Green
</option>
<option value="blue">
Blue
</option>
</select>
</div>
</div>
options can be a simple associative array where keys are the HTML values and values are the related labels. But each value in the options can also be an object, holding a few attributes to better describe each item. Among them:
label
- the label to displaydisabled
- set true if the option is not selectablechildren
- an equivalent array of options, to be used to render optgroups@php
$options = [
'first' => (object) [
'label' => 'First',
],
'second' => (object) [
'label' => 'Second',
'disabled' => true,
],
'third' => (object) [
'label' => 'Third',
'children' => [
'third_1' => (object) [
'label' => 'Third / First',
],
'third_2' => (object) [
'label' => 'Third / Second',
'disabled' => true,
],
'third_3' => (object) [
'label' => 'Third / Third',
],
],
],
];
@endphp
<x-larastrap::select label="Choose One" :options="$options" />
<div class="row mb-3">
<label for="select-dff30e837ed1b0478eea4e3f114c18c0" class="col-4 col-form-label">Choose One</label>
<div class="col-8">
<select class="form-select" name="i6NYpZnO1Y">
<option value="first">
First
</option>
<option value="second" disabled>
Second
</option>
<optgroup label="Third">
<option value="third_1">
Third / First
</option>
<option value="third_2" disabled>
Third / Second
</option>
<option value="third_3">
Third / Third
</option>
</optgroup>
</select>
</div>
</div>
The x-larastrap::select-model variant permits to use objects (eventually: Eloquent models) as options.
@php
$users = App\Models\User::inRandomOrder()->take(3)->get();
@endphp
<x-larastrap::select-model label="Select a User" :options="$users" />
<div class="row mb-3">
<label for="select-4939b66ee43bfcb67864c768fb0585f1" class="col-4 col-form-label">Select a User</label>
<div class="col-8">
<select class="form-select" name="Jig4Pyg1Iz">
<option value="3">
Dr. Darrin Hamill
</option>
<option value="10">
Dr. Gardner Medhurst
</option>
<option value="1">
Prof. Ethan Schmitt III
</option>
</select>
</div>
</div>
The -model
variants are able to set their own value accordingly to the Eloquent relationships of the reference entity (the one assigned as obj
of the parent x-larastrap::form
), as long as their name
matches the attribute to which the relationship itself is exposed.
@php
// In this example, the User model has a "boss" relationship defined as
//
// public function boss()
// {
// return $this->belongsTo(User::class);
// }
$ref = App\Models\User::has('boss')->inRandomOrder()->first();
$bosses = App\Models\User::doesntHave('boss')->orderBy('name', 'asc')->get();
@endphp
<x-larastrap::form :obj="$ref">
<x-larastrap::select-model name="boss" :label="sprintf('Select a Boss for %s', $ref->name)" :options="$bosses" />
</x-larastrap::form>
<form id="form-6364d3f0f495b6ab9dcf8d3b5c6e0b01" class="" method="post" name="form-6364d3f0f495b6ab9dcf8d3b5c6e0b01">
<input type="hidden" name="_token" value="JOcCnA7SWVUJGxoUbx0HMFfMvivZwLCKFuh9eYkK">
<div class="row mb-3">
<label for="select-b1cd86b582c2cd0bdf3bbc65678e006b" class="col-4 col-form-label">Select a Boss for Aiden Osinski</label>
<div class="col-8">
<select class="form-select" name="boss">
<option value="7">
Amaya Beer
</option>
<option value="9" selected>
Carolyne Bradtke
</option>
<option value="10">
Dr. Gardner Medhurst
</option>
<option value="6">
Dr. Pasquale Borer IV
</option>
<option value="8">
Miss Earnestine Lowe
</option>
</select>
</div>
</div>
<div class="col-12 text-end">
<button id="button-c6b7ccbb9d82c01bce681a0af9f6aee7" class="btn btn-primary" type="submit">Save</button>
</div>
</form>
For -model
variant it is assumed that models into the options
collection have an "id" and a "name" attribute to be used (respectively: as the value and the label of each item).
If your models have a different structure, or you want to display different labels, it is possible to pass a translateCallback parameter with a function to translate your actual content: it has to accept a parameter (the model to be translated) and must return an array with two elements (the value and the label of the final selectable option).
@php
$users = App\Models\User::inRandomOrder()->take(3)->get();
$translate = fn($obj) => [$obj->id, $obj->email];
@endphp
<x-larastrap::select-model label="Select a User" :options="$users" :translateCallback="$translate" />
<div class="row mb-3">
<label for="select-dcddc646da85ee9ae1f9b18692eb331b" class="col-4 col-form-label">Select a User</label>
<div class="col-8">
<select class="form-select" name="WUCK7j2Epe">
<option value="3">
lincoln.davis@example.net
</option>
<option value="2">
cwalsh@example.com
</option>
<option value="10">
pokon@example.com
</option>
</select>
</div>
</div>
The -model
variants have also an extra_options
parameter to define other options aside those collected in options
. This is useful to add a "void" item to be choosen by the user.
@php
$users = App\Models\User::inRandomOrder()->take(3)->get();
@endphp
<x-larastrap::select-model label="Select a User" :options="$users" :extra_options="[0 => 'None']" value="0" />
<div class="row mb-3">
<label for="select-4e175a2c07a3fd0395dd6fbba5b7c22a" class="col-4 col-form-label">Select a User</label>
<div class="col-8">
<select class="form-select" name="gYPr5rb3hh">
<option value="0" selected>
None
</option>
<option value="1">
Prof. Ethan Schmitt III
</option>
<option value="5">
Mr. Cyril Adams
</option>
<option value="10">
Dr. Gardner Medhurst
</option>
</select>
</div>
</div>