Selects

options

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-1c54de06058f8c83b860360e9b67ce18" class="col-4 col-form-label">Choose One</label>
  <div class="col-8">
    <select id="select-1c54de06058f8c83b860360e9b67ce18" class="form-select" name="P92QmjlusI">
      <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 display
  • disabled - set true if the option is not selectable
  • children - 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-314974040623691bd8a03e2205db8e64" class="col-4 col-form-label">Choose One</label>
  <div class="col-8">
    <select id="select-314974040623691bd8a03e2205db8e64" class="form-select" name="Z2AZiT7rCw">
      <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>

With Models

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-d968279810834dc3ac391ab183b38395" class="col-4 col-form-label">Select a User</label>
  <div class="col-8">
    <select id="select-d968279810834dc3ac391ab183b38395" class="form-select" name="YvSA7iwd8T">
      <option value="5">
        Mr. Cyril Adams
      </option>
      <option value="4">
        Aiden Osinski
      </option>
      <option value="3">
        Dr. Darrin Hamill
      </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-d67d8ab4f4c10bf22aa353e27879133c" class="" method="post" name="form-d67d8ab4f4c10bf22aa353e27879133c">
  <input type="hidden" name="_token" value="HGql1oOGaxvNwCgxBSqXyalpHjC6LiEBLjxmURnR">
  <div class="row mb-3">
    <label for="select-ea55ab75203e06bae0826f8a6591bd10" class="col-4 col-form-label">Select a Boss for Mr. Randall Hyatt</label>
    <div class="col-8">
      <select id="select-ea55ab75203e06bae0826f8a6591bd10" class="form-select" name="boss">
        <option value="7" selected>
          Amaya Beer
        </option>
        <option value="9">
          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-60b36f6383b9ecaf1d1015ee119bfd62" 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-b4d656e67cc5f4e38af4a23829b33b25" class="col-4 col-form-label">Select a User</label>
  <div class="col-8">
    <select id="select-b4d656e67cc5f4e38af4a23829b33b25" class="form-select" name="JJeQ3fEpJ0">
      <option value="5">
        henri86@example.net
      </option>
      <option value="4">
        estel.barrows@example.org
      </option>
      <option value="3">
        lincoln.davis@example.net
      </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-23620f62bfa6bb30ebd8b170a5b1b35f" class="col-4 col-form-label">Select a User</label>
  <div class="col-8">
    <select id="select-23620f62bfa6bb30ebd8b170a5b1b35f" class="form-select" name="P9P7Qtk12J">
      <option value="0" selected>
        None
      </option>
      <option value="8">
        Miss Earnestine Lowe
      </option>
      <option value="5">
        Mr. Cyril Adams
      </option>
      <option value="10">
        Dr. Gardner Medhurst
      </option>
    </select>
  </div>
</div>

Larastrap