この内容は古いバージョンです。最新バージョンを表示するには、戻るボタンを押してください。
バージョン:5
ページ更新者:guest
更新日時:2026-06-11 07:29:05

タイトル: Selectの結果の最初もしくは最後に任意の値を追加する方法
SEOタイトル: Laravel コレクション先頭/末尾追加完全ガイド|select 結果に「選択してください」を加える

この本文には実行可能なタグ等が含まれるため、安全のためHTMLソースとして表示しています。本文データは変更していません。

<table border="1" cellpadding="6" cellspacing="0" style="border-collapse:collapse;background:#fffbea;border:1px solid #f4c430;margin:1em 0;width:100%"><tr><td style="background:#f4c430;color:#000;font-weight:bold;padding:8px">この記事の要点</td></tr><tr><td style="padding:10px"><ul><li><strong>先頭に追加</strong>: <code>$collection-&gt;prepend($value, $key)</code></li><li><strong>末尾に追加</strong>: <code>$collection-&gt;push($value)</code> または <code>put($key, $value)</code></li><li><strong>結合</strong>: <code>$collection-&gt;concat([...])</code> / <code>merge([...])</code></li><li>HTML <code>&lt;select&gt;</code> の先頭に「選択してください」を入れる定番パターン</li><li>Eloquent + Collection で<strong>連想配列 (id => name)</strong> を作るときは <code>pluck()</code> → <code>prepend()</code></li></ul></td></tr></table>
<h2>結論: select 用配列の作り方</h2>

<p>Laravel で <code>&lt;select&gt;</code> のオプション一覧を作るとき、先頭に「選択してください」のような空オプションを入れたいケースは頻出。コレクションの <code>prepend()</code> を使うのが定石です:</p>
<pre style="background:#1e293b;color:#e2e8f0;padding:1em;border-radius:6px;overflow-x:auto;line-height:1.5"><code class="language-php">use App\Models\Category;

// id =&gt; name の連想配列を取得
$categories = Category::pluck(&apos;name&apos;, &apos;id&apos;);
// [1 =&gt; &apos;雑貨&apos;, 2 =&gt; &apos;食品&apos;, 3 =&gt; &apos;家電&apos;]

// 先頭に「選択してください」を追加
$options = $categories-&gt;prepend(&apos;-- 選択してください --&apos;, &apos;&apos;);
// [&apos;&apos; =&gt; &apos;-- 選択してください --&apos;, 1 =&gt; &apos;雑貨&apos;, 2 =&gt; &apos;食品&apos;, 3 =&gt; &apos;家電&apos;]

// Blade で使う
// {{ Form::select(&apos;category_id&apos;, $options, null, [&apos;class&apos; =&gt; &apos;form-control&apos;]) }}</code></pre>
<h2>Collection の追加系メソッド一覧</h2>

<table border="1" cellpadding="6" cellspacing="0" style="border-collapse:collapse;width:100%">
<thead><tr style="background:#e3f2fd"><th>メソッド</th><th>位置</th><th>キー指定</th><th>例</th></tr></thead>
<tbody>
<tr><td><code>prepend($value, $key = null)</code></td><td><strong>先頭</strong></td><td>可</td><td>$c->prepend('A', 0)</td></tr>
<tr><td><code>push($value)</code></td><td><strong>末尾</strong></td><td>不可(自動採番)</td><td>$c->push('Z')</td></tr>
<tr><td><code>put($key, $value)</code></td><td>末尾(または上書き)</td><td>必須</td><td>$c->put('z', 'Z')</td></tr>
<tr><td><code>add($value)</code></td><td>末尾</td><td>不可</td><td>$c->add('Z')</td></tr>
<tr><td><code>concat($source)</code></td><td>末尾に結合</td><td>キー振り直し</td><td>$c->concat([10, 20])</td></tr>
<tr><td><code>merge($items)</code></td><td>末尾結合(キー保持)</td><td>同キーは上書き</td><td>$c->merge(['a' => 1])</td></tr>
<tr><td><code>union($items)</code></td><td>末尾結合(既存優先)</td><td>同キーは無視</td><td>$c->union(['a' => 9])</td></tr>
</tbody>
</table>

<h2>prepend の使い方</h2>
<pre style="background:#1e293b;color:#e2e8f0;padding:1em;border-radius:6px;overflow-x:auto;line-height:1.5"><code class="language-php">use Illuminate\Support\Collection;

$c = collect([1 =&gt; &apos;A&apos;, 2 =&gt; &apos;B&apos;, 3 =&gt; &apos;C&apos;]);

// キー指定なし(自動で 0 番)
$c-&gt;prepend(&apos;Z&apos;);
// [0 =&gt; &apos;Z&apos;, 1 =&gt; &apos;A&apos;, 2 =&gt; &apos;B&apos;, 3 =&gt; &apos;C&apos;]

// キー指定あり
$c-&gt;prepend(&apos;Z&apos;, &apos;zero&apos;);
// [&apos;zero&apos; =&gt; &apos;Z&apos;, 1 =&gt; &apos;A&apos;, 2 =&gt; &apos;B&apos;, 3 =&gt; &apos;C&apos;]

// 空文字キーで select 用先頭に
$c-&gt;prepend(&apos;選択してください&apos;, &apos;&apos;);
// [&apos;&apos; =&gt; &apos;選択してください&apos;, 1 =&gt; &apos;A&apos;, 2 =&gt; &apos;B&apos;, 3 =&gt; &apos;C&apos;]</code></pre>
<h2>push / put / add の違い</h2>
<pre style="background:#1e293b;color:#e2e8f0;padding:1em;border-radius:6px;overflow-x:auto;line-height:1.5"><code class="language-php">$c = collect([&apos;a&apos; =&gt; 1, &apos;b&apos; =&gt; 2]);

// push : 末尾に追加。キー指定不可(連番)
$c-&gt;push(99);
// [&apos;a&apos; =&gt; 1, &apos;b&apos; =&gt; 2, 0 =&gt; 99]

// add : push と同等(メソッド名違いの別名)
$c-&gt;add(99);

// put : キー指定必須。同じキーがあれば上書き
$c-&gt;put(&apos;c&apos;, 3);
// [&apos;a&apos; =&gt; 1, &apos;b&apos; =&gt; 2, &apos;c&apos; =&gt; 3]

$c-&gt;put(&apos;a&apos;, 999);  // 上書き
// [&apos;a&apos; =&gt; 999, &apos;b&apos; =&gt; 2, ...]</code></pre>
<h2>concat / merge / union の違い</h2>
<pre style="background:#1e293b;color:#e2e8f0;padding:1em;border-radius:6px;overflow-x:auto;line-height:1.5"><code class="language-php">$a = collect([&apos;x&apos; =&gt; 1, &apos;y&apos; =&gt; 2]);
$b = [&apos;y&apos; =&gt; 20, &apos;z&apos; =&gt; 30];

// concat : キーを振り直して結合
$a-&gt;concat($b);
// [0 =&gt; 1, 1 =&gt; 2, 2 =&gt; 20, 3 =&gt; 30]

// merge : キー保持、同キーは引数側で上書き
$a-&gt;merge($b);
// [&apos;x&apos; =&gt; 1, &apos;y&apos; =&gt; 20, &apos;z&apos; =&gt; 30]

// union : キー保持、同キーは元側を優先
$a-&gt;union($b);
// [&apos;x&apos; =&gt; 1, &apos;y&apos; =&gt; 2, &apos;z&apos; =&gt; 30]</code></pre>
<h2>HTML <select> を組み立てる</h2>

<h3>純粋 HTML 出力</h3>
<pre style="background:#1e293b;color:#e2e8f0;padding:1em;border-radius:6px;overflow-x:auto;line-height:1.5"><code class="language-php">// Controller
$options = Category::pluck(&apos;name&apos;, &apos;id&apos;)
    -&gt;prepend(&apos;-- 選択してください --&apos;, &apos;&apos;);

return view(&apos;items.create&apos;, compact(&apos;options&apos;));</code></pre><pre style="background:#1e293b;color:#e2e8f0;padding:1em;border-radius:6px;overflow-x:auto;line-height:1.5"><code class="language-php">{{-- Blade: items/create.blade.php --}}
&lt;select name=&quot;category_id&quot; class=&quot;form-control&quot;&gt;
@foreach ($options as $value =&gt; $label)
    &lt;option value=&quot;{{ $value }}&quot; @selected(old(&apos;category_id&apos;) == $value)&gt;
        {{ $label }}
    &lt;/option&gt;
@endforeach
&lt;/select&gt;</code></pre>
<h3>Laravel Collective Form 利用時</h3>
<pre style="background:#1e293b;color:#e2e8f0;padding:1em;border-radius:6px;overflow-x:auto;line-height:1.5"><code class="language-php">{{-- composer require laravelcollective/html --}}
{!! Form::select(&apos;category_id&apos;, $options, old(&apos;category_id&apos;), [
    &apos;class&apos; =&gt; &apos;form-control&apos;,
]) !!}

{{-- $options は連想配列 / Collection どちらも可 --}}</code></pre>
<h2>optgroup(分類された select)</h2>
<pre style="background:#1e293b;color:#e2e8f0;padding:1em;border-radius:6px;overflow-x:auto;line-height:1.5"><code class="language-php">// 親子構造で送る
$groups = [
    &apos;食品&apos; =&gt; [1 =&gt; &apos;パン&apos;, 2 =&gt; &apos;米&apos;],
    &apos;雑貨&apos; =&gt; [3 =&gt; &apos;ノート&apos;, 4 =&gt; &apos;ペン&apos;],
];</code></pre><pre style="background:#1e293b;color:#e2e8f0;padding:1em;border-radius:6px;overflow-x:auto;line-height:1.5"><code class="language-php">&lt;select name=&quot;item_id&quot;&gt;
    &lt;option value=&quot;&quot;&gt;-- 選択してください --&lt;/option&gt;
@foreach ($groups as $groupLabel =&gt; $items)
    &lt;optgroup label=&quot;{{ $groupLabel }}&quot;&gt;
        @foreach ($items as $id =&gt; $name)
            &lt;option value=&quot;{{ $id }}&quot;&gt;{{ $name }}&lt;/option&gt;
        @endforeach
    &lt;/optgroup&gt;
@endforeach
&lt;/select&gt;</code></pre>
<h2>Eloquent + Collection の合わせ技</h2>
<pre style="background:#1e293b;color:#e2e8f0;padding:1em;border-radius:6px;overflow-x:auto;line-height:1.5"><code class="language-php">use App\Models\Prefecture;

// 都道府県の select オプション(先頭に空欄)
$prefs = Prefecture::orderBy(&apos;code&apos;)
    -&gt;pluck(&apos;name&apos;, &apos;code&apos;)
    -&gt;prepend(&apos;-- 選択してください --&apos;, &apos;&apos;);

// 「その他」を末尾に追加
$prefs-&gt;put(&apos;99&apos;, &apos;その他&apos;);

// 結果
// [&apos;&apos; =&gt; &apos;-- 選択してください --&apos;,
//  &apos;01&apos; =&gt; &apos;北海道&apos;, &apos;02&apos; =&gt; &apos;青森県&apos;, ..., &apos;47&apos; =&gt; &apos;沖縄県&apos;,
//  &apos;99&apos; =&gt; &apos;その他&apos;]</code></pre>
<h2>@forelse で「データなし」のとき</h2>
<pre style="background:#1e293b;color:#e2e8f0;padding:1em;border-radius:6px;overflow-x:auto;line-height:1.5"><code class="language-php">{{-- データが空のときは「カテゴリがありません」を表示 --}}
@forelse ($categories as $cat)
    &lt;option value=&quot;{{ $cat-&gt;id }}&quot;&gt;{{ $cat-&gt;name }}&lt;/option&gt;
@empty
    &lt;option value=&quot;&quot; disabled&gt;カテゴリがありません&lt;/option&gt;
@endforelse</code></pre>
<h2>キーを文字列で揃えるテクニック</h2>

<p>HTML form の value は<strong>常に文字列</strong>で送られるため、id を数値で持っていると <code>old()</code> の比較に <code>==</code> が必要です。型を揃えると安心:</p>
<pre style="background:#1e293b;color:#e2e8f0;padding:1em;border-radius:6px;overflow-x:auto;line-height:1.5"><code class="language-php">$options = Category::pluck(&apos;name&apos;, &apos;id&apos;)
    -&gt;mapWithKeys(fn ($name, $id) =&gt; [(string)$id =&gt; $name])
    -&gt;prepend(&apos;-- 選択してください --&apos;, &apos;&apos;);</code></pre>
<h2>FAQ</h2>

<p><strong>Q: 配列のまま末尾に値を追加したい</strong><br />
A: PHP 配列なら <code>$arr[] = $value</code> で末尾追加、<code>array_unshift($arr, $value)</code> で先頭追加。Collection に変換せず素のままでも可。</p>

<p><strong>Q: prepend のキーが <code>0</code> や <code>''</code> で被ったらどうなる?</strong><br />
A: 既存キーが上書きされます。意図しないなら明示的にユニークキー(<code>'__placeholder__'</code> 等)を指定。</p>

<p><strong>Q: select の選択値を保持したい (old()) </strong><br />
A: <code>@selected(old('category_id') == $value)</code> または<code>{{ Form::select(..., old('category_id') ?? $model-&gt;category_id, ...) }}</code>。型に注意。</p>

<p><strong>Q: ページネーション結果に「全件」を追加したい</strong><br />
A: ページネーションオブジェクトは Collection ではないので、<code>-&gt;items()</code> で配列を取り出してから操作するか、Collection で先に組み立てた後ページング。</p>