php Laravel 刀片复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26973442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:07:27  来源:igfitidea点击:

Laravel blade check box

phplaravelcheckboxlaravel-blade

提问by lggg3

I want to set check-boxes state from database, so I write,

我想从数据库设置复选框状态,所以我写,

{{ Form::checkbox('asap', null, $offer->asap) }}

But if I want to set 'id' to the check-box like

但是,如果我想将“id”设置为复选框,例如

{{ Form::checkbox('asap', null, $offer->ASAP, array('id'=>'asap')) }}

It always set my check-box state to true. (Before user select it)

它总是将我的复选框状态设置为 true。(在用户选择它之前)

So question how set 'id' in blade check-boxes when check-box state is set before user select it?

那么,在用户选择之前设置复选框状态时,如何在刀片复选框中设置“id”?

采纳答案by Marcin Nabia?ek

3rd argument decides whether checkbox is checked or not. So probably $offer->ASAP(or $offer->asapis true (or not false or not null). If you want to to make checkbox unchecked either set it to false, or don't use 3rd argument (set to to null or false):

第三个参数决定是否选中复选框。所以可能$offer->ASAP(或$offer->asap为真(或不为假或不为空)。如果您想取消选中复选框,请将其设置为假,或不使用第三个参数(设置为空或假):

{{ Form::checkbox('asap',null,null, array('id'=>'asap')) }}

EDIT

编辑

Another possibility is that you have on your page some custom JavaScript code that finds element by asapid and checks this checkbox. So when you don't set id, JavaScript cannot check it, but when you set this id, the checkbox will be checked by JavaScript.

另一种可能性是您的页面上有一些自定义 JavaScript 代码,这些代码通过asapid查找元素并选中此复选框。所以当你不设置 id 时,JavaScript 无法检查它,但是当你设置这个 id 时,复选框会被 JavaScript 选中。

回答by Mohamed Salem Lamiri

I know this question was answered before, in this one I am going to explain step by step how to implement checkboxes with Laravel/blade with different cases ...

我知道这个问题之前已经回答过,在这个问题中,我将逐步解释如何在不同情况下使用 Laravel/blade 实现复选框......

So in order to load your checkboxes from the database or to test if a checkbox is checked :

因此,为了从数据库加载复选框或测试复选框是否被选中:

Firstof all you need to understand how it works, as @itachi mentioned :

首先你需要了解它是如何工作的,正如@itachi 提到的:

{{ Form::checkbox( 1st argument, 2nd argument, 3rd argument, 4th argument ) }}

{{ Form::checkbox( 第一个参数,第二个参数,第三个参数,第四个参数) }}

  • First argument : name
  • Second argument : value
  • Third argument : checked or not checked this takes: trueor false
  • Fourth argument : additional attributes (e.g., checkbox css classe)
  • 第一个参数:名称
  • 第二个参数:值
  • 第三个参数:检查或不检查这需要:
  • 第四个参数:附加属性(例如,复选框css classe

Example :

例子 :

{{ Form::checkbox('admin') }} 
//will produces the following HTML
<input name="admin" type="checkbox" value="1">

{{ Form::checkbox('admin', 'yes', true) }}
//will produces the following HTML
<input checked="checked" name="admin" type="checkbox" value="yes">

How to get checkboxes values ?( in your controller )

如何获取复选框值?(在您的控制器中)

Methode 1 :

方法一:

public function store(UserCreateRequest $request)
{

   $my_checkbox_value = $request['admin'];

  if($my_checkbox_value === 'yes')
     //checked
  else
     //unchecked
  ...
}

Methode 2 :

方法二:

if (Input::get('admin') === 'yes') {
    // checked
} else {
    // unchecked
}

Note : you need to assign a default value for unchecked box :

注意:您需要为未选中的框分配一个默认值:

if(!$request->has('admin'))
{
    $request->merge(['admin' => 0]);
}

this is nice right ? but how could we set checked boxes in our view ?

这很好吧?但是我们如何在我们的视图中设置复选框?

For good practice I suggest using Form::modelwhen you create your form this will automatic fill input values that have the same names as the model (as well as using different blade Form:: inputs ..)

对于良好的实践,我建议Form::model您在创建表单时使用,这将自动填充与模型同名的输入值(以及使用不同的刀片 Form::inputs ..)

{!! Form::model( $user, ['route' => ['user.update', $user->id], 'method' => 'put' ]) !!}
    {!! Form::checkbox('admin', 1, null) !!}
{!! Form::close() !!}

You can also get it like this :

你也可以这样得到:

{{ Form::checkbox('admin',null, $user->admin) }}

Okey now how to deal with :

好吧现在如何处理:

  • multiples checkboxes
  • Add css classe
  • Add checkbox id
  • Add label
  • 多个复选框
  • 添加css类
  • 添加复选框ID
  • 添加标签

let's say we want to get working days from our database Working days

假设我们想从我们的数据库中获取工作日 工作日

$working_days = array( 0 => 'Mon', 1 => 'Tue', 2 => 'Wed', 
                       3 => 'Thu', 4 => 'Fri', 5 => 'Sat', 6 => 'Sun' );

@foreach ( $working_days as $i => $working_day )
{!! Form::checkbox( 'working_days[]', 
                  $working_day,
                  !in_array($working_days[$i],$saved_working_days),
                  ['class' => 'md-check', 'id' => $working_day] 
                  ) !!}
{!! Form::label($working_day,  $working_day) !!}
@endforeach
//$saved_working_days is an array of days (have 7 days, checked & unchecked)

I've spent sometime to figure out how to deal with multiple checkboxes I hope this can help someone :)

我花了一些时间来弄清楚如何处理多个复选框我希望这可以帮助某人:)

回答by itachi

in FormBuilder.php

FormBuilder.php

    public function checkbox($name, $value = 1, $checked = null, $options = array())
{
    return $this->checkable('checkbox', $name, $value, $checked, $options);
}
  • 1st param: name
  • 2nd : value
  • 3rd : checked or not (i.e. null, false or true)
  • 4th : attributes.
  • 第一个参数:名称
  • 第二:价值
  • 第三:检查与否(即空,假或真)
  • 第四:属性。

{{ Form::checkbox('asap',null,$offer->ASAP, array('id'=>'asap')) }}

{{ Form::checkbox('asap',null,$offer->ASAP, array('id'=>'asap')) }}

your order is wrong.

你的顺序错了。

it should be,

它应该是,

{{ Form::checkbox('asap',$offer->ASAP, null, array('id'=>'asap')) }}

回答by Cherma Ramalho

Good luck!

祝你好运!

 <div class="togglebutton">
       <label>
       @if ($programmer->play === 1)
          <input type="checkbox" name="play" checked="">
       @else
          <input type="checkbox" name="play" {{ old('play') ? 'checked' : '' }} >
       @endif
          Play
      </label>

 </div>