Laravel发布单选按钮

时间:2019-05-07 08:19:40

标签: laravel forms button

我想将单选按钮的值发送给该类,问题在于每次使用的名称都不相同,因为我有一个id最终将其分组到视图中,如何引用该名称? >

@foreach($users as $user)
<tr>
    <td>{{ $user->id }}</td>
    <td>{{ $user->name }}</td>
    <td>{{ $user->email }}</td>

    <td>
        <form method="post" action="updateprivileges/{{$user->id}}">
            @csrf 
            <div class="row">
                <div class="col-4">
                    <input type="radio" value="1" class="flat" name="privileges_{{ $user->id }}" {{ $user->privileges[0]['roles_id'] == '1' ? 'checked' : '' }} > <div class="uprawnienia-margin">Administrator</div>
                </div>
                <div class="col-4">
                    <input type="radio" value="2" class="flat" name="privileges_{{ $user->id }}" {{ $user->privileges[0]['roles_id'] == '2' ? 'checked' : '' }} > <div class="uprawnienia-margin">Serwisant</div>
                </div>
                <div class="col-4">
                    <input type="radio" value="3" class="flat" name="privileges_{{ $user->id }}" {{ $user->privileges[0]['roles_id'] == '3' ? 'checked' : '' }} > <div class="uprawnienia-margin">Monter</div>
                </div>
            </div>
            <input type="submit" name="add" class="btn btn-primary input-lg" value="Przeslij" />
        </form>
    </td>
</tr>
@endforeach

1 个答案:

答案 0 :(得分:0)

您具有动态名称属性。您应该将其更改为静态值,以便始终可以在接收有效负载的控制器中接收正确的值。并且不要忘记使用正确的HTTP Method

<form method="post" action="{{ URL::to('/updateprivileges/' . $user->id)}}">
    @csrf

    <input type="radio" name="privilege" value="1"> Administrator</br>
    <input type="radio" name="privilege" value="2"> Serwisant</br>
    <input type="radio" name="privilege" value="3"> Monter</br>
    <input type="submit" value="Przeslij">
</form>

routes/web.php中声明路线:

// put method
Route::post('updateprivileges/{id}', 'UserController@updatePrivileges');

现在,您的UserControllerprivilege中收到了Request $request变量:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{
    public function updatePrivileges(Request $request, $id)
    {
        $privilege = $request->input('privilege');
        dd($privilege); // dumps $privilege and dies
        // more code, perhaps save the users privilege
    }
}

我希望这会有所帮助。

您可能想签出Laravel Method SpoofingPut, Post, Patch。由于您通过ID认识了用户,因此您可能希望切换到PUTPATCH方法。