Как создать рейтинговый пост? - PullRequest
1 голос
/ 08 октября 2019

У меня есть список уроков, и рядом с каждым уроком будет input type = "radio", чтобы оценить этот урок от 1 до 10 баллов. Я хочу нажать на переключатель, чтобы получить значение этой кнопки и сохранить значение в базе данных. Но, похоже, это не сработает.

<tbody>
   @foreach($rates as $rate)
   <form method="post" action="{{url('/rating')}}">
   <input type="hidden" name="rateId" value="{{$rate->id}}">
      {{ csrf_field() }}
        <tr style="display: flex;">
          <td class="col-md-1"></td>
          <td class="col-md-6" style="text-align: left;"><a href="{{url('/bai-giang/'.$rate->id)}}">{{$rate->title}}</a></td>
          <td class="col-md-3" style="text-align: center;"></td>
          <td class="col-md-2">
          <div align="center" style="display: inline-flex;">
          <input type="radio" name="rating" value="1" onclick="return confirmAction()">
          <input type="radio" name="rating" value="2" onclick="return confirmAction()">
          <input type="radio" name="rating" value="3" onclick="return confirmAction()">
          <input type="radio" name="rating" value="4" onclick="return confirmAction()">
          <input type="radio" name="rating" value="5" onclick="return confirmAction()">
          <input type="radio" name="rating" value="6" onclick="return confirmAction()">
          <input type="radio" name="rating" value="7" onclick="return confirmAction()">
          <input type="radio" name="rating" value="8" onclick="return confirmAction()">
          <input type="radio" name="rating" value="9" onclick="return confirmAction()">
         <input type="radio" name="rating" value="10" onclick="return confirmAction()">
          </div>
       </td>
  </tr>
</form>
@endforeach
</tbody>

Моя функция сохранения значения с помощью переключателя


    public function rating(Request $req)
        {
            $id = $req->rateId;
            $lesson = Lesson::find($id);
            $lesson->rating = $req->rating;
            $lesson->save();
            return redirect()->back()->with('success', 'Rating successful');
        }

1 Ответ

0 голосов
/ 08 октября 2019

Свойство name в теге input должно быть одинаковым для всех переключателей, и вы должны использовать это имя при получении выбранного значения.

Пример:

<input type="radio" name="rating" value="3" onclick="return confirmAction()">
<input type="radio" name="rating" value="4" onclick="return confirmAction()">
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...