ふたりはララベル (Laravel PHP Framework)

PHPフレームワークのLaravelの体験記を書いていきます。こんなタイトルのブログですが萌え系アニメは一秒たりとも観たことがありません。

Laravelでラジオボックス

Laravelでラジオボックスの情報を取得したときのメモ。

Blade側

@foreach($comments as $comment)
<div class="inline-group">
<label class="radio">
  <input type="radio" name="priority_{{ $comment->id }}" value='abc'>
  <i class="rounded-x"></i>abc
</label>
<label class="radio">
  <input type="radio" name="priority_{{ $comment->id }}" value='efg'>
  <i class="rounded-x"></i>efg
</label>
<label class="radio">
  <input type="radio" name="priority_{{ $comment->id }}" value='hij'>
  <i class="rounded-x"></i>hij
</label>
</div>
@endforeach

コントローラー側。

$inputs = Input::all();

foreach ($inputs as $key => $value) {

  if (strpos($key, 'priority_') !== FALSE) {

    $id = str_replace('priority_', '', $key);

    $comment = Comment::find($id);

    $comment->priority = $value;

    $comment->save();
  
  }

}