laravel 拉拉维尔 | 编辑页面上的复选框状态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36338088/
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
Laravel | Checkbox state on edit page
提问by nclsvh
I have a create page for a new Case in Laravel 5, where I can link services to the case. The services are checkboxes in my createCaseForm. The services are stored in the DB so I write to a ManyToMany table to have many Cases which have many services.
我在 Laravel 5 中有一个新案例的创建页面,我可以在其中将服务链接到案例。这些服务是我的 createCaseForm 中的复选框。这些服务存储在数据库中,所以我写入了一个 ManyToMany 表来拥有许多具有许多服务的案例。
Example: When I create case1, I check the boxes for service1 and service3. So now the manyToMany table is filled in correctly. Now when I go to the edit page for the case I want the checkbox1 and checkbox3 to be checked by default, because they are already linked in the manyToMany table.
示例:当我创建 case1 时,我选中了 service1 和 service3 的框。所以现在manyToMany表被正确填充。现在,当我转到案例的编辑页面时,我希望默认选中 checkbox1 和 checkbox3,因为它们已经链接到 manyToMany 表中。
I know you can use something like Input::old('username'); for textfields, but I don't know how to do this for a checkbox.
我知道你可以使用类似 Input::old('username'); 的东西。对于文本字段,但我不知道如何为复选框执行此操作。
I do use LaravelCollectivemost of the time, but for these checkboxes I didn't. So preferably I would like a HTML solution, IF POSSIBLE ofcourse.
大多数时候我确实使用LaravelCollective,但对于这些复选框我没有。所以最好我想要一个 HTML 解决方案,如果可能的话。
Thanks
谢谢
采纳答案by Captain Hypertext
The quick answer
快速回答
@foreach($services as $service)
<input type="checkbox" name="services[]" value="{{ $service->id }}"
@if (count($case->services->where('id', $service->id)))
checked
@endif>
@endforeach
I'm assuming you have a many to many eloquent relationship between Cases and Services. So that where
is a collection method, not a query builder. If it finds nothing, it will return an empty collection, which is truthy by itself (hence the count
function).
我假设您在案例和服务之间有很多雄辩的关系。所以这where
是一个集合方法,而不是一个查询构建器。如果它什么也没找到,它将返回一个空集合,它本身就是真的(因此是count
函数)。
But for a more concise approach, if you're just looking at one Case at a time, I would go ahead and get a simple array of all the Service IDs that the case has and pass it into the view.
但是对于更简洁的方法,如果您一次只查看一个案例,我会继续获取案例具有的所有服务 ID 的简单数组并将其传递到视图中。
// The eloquent way
$case_services = $case->services->toArray();
// Otherwise, just query for all service IDs
$case_services = DB::table('case_services')->where('etc...')
// You'll end up with something like [24, 47, 103, 287]
Then in your view, you can just run something like
然后在您看来,您可以运行类似
@foreach($services as $s)
<input type="checkbox" name="{{ $s->name }}" value="{{ $s->service_id }}"
@if( in_array($s->service_id, $case_services) ) checked="1" @endif />
@endforeach
I just like the second solution because it's easier to tell what's going on, and is more predictable to me.
我只是喜欢第二种解决方案,因为它更容易说明发生了什么,并且对我来说更可预测。
回答by Qazi
This is sample code, you can do this, If you are using Blade template, then do this, 3rd parameter is true/false to checked/un-checked the checkbox
这是示例代码,您可以执行此操作,如果您使用的是 Blade 模板,则执行此操作,第 3 个参数为 true/false 以选中/取消选中复选框
{!! Form::checkbox('agree', 1, ($model->checkbox ==1?true:null), ['class' => 'field']) !!}
here you are passing your model DB stored value to checkbox (in 3rd param)
在这里,您将模型 DB 存储值传递给复选框(在第三个参数中)