2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

星5段階評価プルダウンメニューの作成手順

Last updated at Posted at 2020-02-11

星5段階評価プルダウンメニューを導入したのでその手順をメモします

configディレクトリ下にデータをつくる

score.php
<?php
  return array(
    '1' => '⭐️',
    '2' => '⭐️⭐️',
    '3' => '⭐️⭐️⭐️',
    '4' => '⭐️⭐️⭐️⭐️',
    '5' => '⭐️⭐️⭐️⭐️⭐️',
  );
?>

ビューでプルダウンを表示する

create.blade.php
<select type="text" name="review">
    @foreach(config('score') as $key => $score)
        <option value="{{ $key }}">{{ $score }}</option>
    @endforeach
</select>

プルダウンで選択して送信したデータを取得する

viewの記述(if文を使用する場合)

detail.blade.php
    @if (取得したいデータ == 1)
        <p>{{ '⭐️' }}</p>
    @elseif (取得したいデータ == 2)
        <p>{{ '⭐️⭐️' }}</p>
    @elseif (取得したいデータ == 3)
        <p>{{ '⭐️⭐️⭐️' }}</p>
    @elseif (取得したいデータ == 4)
        <p>{{ '⭐️⭐️⭐️⭐️' }}</p>
    @elseif (取得したいデータ == 5)
        <p>{{ '⭐️⭐️⭐️⭐️⭐️' }}</p>
    @endif

viewの記述(switch文を使用する場合)

detail.blade.php
@switch (取得したいデータ)
    @case (1)
        <p>{{ '⭐️' }}</p>
        @break
    @case (2)
        <p>{{ '⭐️⭐️' }}</p>
        @break
    @case (3)
        <p>{{ '⭐️⭐️⭐️' }}</p>
        @break
    @case (4)
       <p>{{ '⭐️⭐️⭐️⭐️' }}</p>
        @break
    @case (5)
        <p>{{ '⭐️⭐️⭐️⭐️⭐️' }}</p>
        @break
@endswitch

viewの記述(1行で済ませたい場合)

detail.blade.php
{{ config('score')[取得したいデータ] }}

参考

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?