Laravel 5,如何测试控制器中是否选中了Checkbox

loi*_*pez 5 php laravel

如果选中复选框,我试图获取:

在我看来 :

 <form data-toggle="validator" data-disable="false" role="form" action="/admin/role/add" method="post">
  <div class="checkbox checkbox-success">
   <input name="test" id="test" type="checkbox" value="test">
   <label for="test" style="padding-left: 15px!important;">test</label>
  </div>
 </form>
 <form data-toggle="validator" data-disable="false" role="form" action="/admin/role/add" method="post">
  {{ csrf_field() }}
  <div class="form-group pull-right">
     <button type="submit" class="btn btn-danger">Submit</button>
  </div>
 </form>
Run Code Online (Sandbox Code Playgroud)

在我的控制器中:

public function createRole(Request $request)
{
    if( $request->has('test')) {
        new \App\Debug\Firephp('test', ['test' => true]);
    }
}
Run Code Online (Sandbox Code Playgroud)

在web.php中:

 Route::post('/admin/role/add', 'AdminController@createRole')
Run Code Online (Sandbox Code Playgroud)

但不起作用.

我该怎么办?

谢谢你的答复.

编辑1:

这是我的形式很差的建设

Peo*_*eon 6

复选框在选中时发送信息,因此您要做的是检查该值是否存在。如果您使用方法 withRequest $request比使用直接输入更容易:

if (isset($request->test)) {
    // checked
}
Run Code Online (Sandbox Code Playgroud)

Laravel 文档:https ://laravel.com/docs/5.0/requests


Kur*_*ucu 6

你可以测试它:

if( Input::get('test', false) ) {
    // there is something for 'test'
} else {
    // there was no 'test' or it was false
}
Run Code Online (Sandbox Code Playgroud)

要么

if( Request::input('test') ) { //...
Run Code Online (Sandbox Code Playgroud)

要么

public function store(Request $request)
{
    if( $request->has('test') ){
        //...
    }
}
Run Code Online (Sandbox Code Playgroud)

https://laravel.com/docs/5.0/requests


Ed *_*nds 5

我相信你真正的问题是你有两种不同的形式.您的复选框采用一种形式,您的提交按钮采用第二种形式.我相信它们都需要采用相同的形式.否则,永远不会返回您的复选框状态,无论其状态如何.

在您的视图中,尝试替换您提供的表单标记:

<form data-toggle="validator" data-disable="false" role="form" action="/admin/role/add" method="post">
    <div class="checkbox checkbox-success">
        <input name="test" id="test" type="checkbox" value="test">
        <label for="test" style="padding-left: 15px!important;">test</label>
    </div>
    {{ csrf_field() }}
    <div class="form-group pull-right">
        <button type="submit" class="btn btn-danger">Submit</button>
    </div>
</form>
Run Code Online (Sandbox Code Playgroud)