Laravel 表单模型绑定和复选框的多对多 Eloquent 关系更新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20179473/
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
Many-to-Many Eloquent relationship update with Laravel Form Model Binding & Checkboxes
提问by Yev
I have 3 tables:
我有3张桌子:
doors
门
- id
- name
- image
- ID
- 姓名
- 图片
colors
颜色
- id
- name
- image
- ID
- 姓名
- 图片
door_colors
门颜色
- id
- door_id
- color_id
- ID
- 门 ID
- 颜色标识
and 2 models with a many-to-many relationship (each door comes in a variety colors, and many colors overlap door-to-door):
和2个多对多关系的模型(每扇门都有多种颜色,许多颜色门对门重叠):
Door Model
门模型
class Door extends Eloquent {
public function colors()
{
return $this->belongsToMany('Color', 'door_colors');
}
}
Color Model
颜色模型
class Color extends Eloquent {
public function doors()
{
return $this->belongsToMany('Door', 'door_colors');
}
}
I want to create a form where I can edit the door, and update the available colors via checkboxes. This is my Admin Doors Controller
我想创建一个表单,我可以在其中编辑门,并通过复选框更新可用颜色。这是我的管理门控制器
class AdminDoorsController extends AdminController {
public function edit($id)
{
$data['door'] = Door::find($id);
$data['colors'] = Color::all();
return View::make('admin/doors/form', $data);
}
}
and the Admin Doors Form View
和Admin Doors 表单视图
{{ Form::model($door) }}
Colors:
@foreach ($colors as $color)
{{ Form::checkbox('colors[]', $color->id) }} {{ $color->name }}
@endforeach
{{ Form::close() }}
Question 1:How do I make it so that as the checkboxes are outputted, the ones with an existing relationship with the current door are checked and the ones without are unchecked.
问题1:如何做到在输出复选框时,与当前门存在关系的勾选,不勾选的不勾选。
Question 2:Once I check the boxes and hit submit, how would I update the relationships? $door->colors()->detach();
to clear all existing ones for this door, then $door->colors()->attach($color_id_array);
to create new ones based on an array of color ids?
问题 2:一旦我选中复选框并点击提交,我将如何更新关系?$door->colors()->detach();
清除这扇门的所有现有门,然后$door->colors()->attach($color_id_array);
根据颜色 ID 数组创建新门?
Any input is appreciated!
任何输入表示赞赏!
回答by user1669496
Question 1: You should pass this into the view that contains your form, though it can also go right in the view, though that's not really best practice. Do something similar to this...
问题 1:您应该将其传递到包含您的表单的视图中,尽管它也可以直接进入视图,尽管这不是真正的最佳实践。做类似的事情...
$checkeds = Door::find(1)->colors()->lists('id');
...where the door you are finding is the door that's being updated. Then before you output the checkbox in the loop, add
...您找到的门就是正在更新的门。然后在循环中输出复选框之前,添加
$checked = in_array($color->id, $checkeds) ? true : false;
Then you would change
然后你会改变
{{ Form::checkbox('colors[]', $color->id) }}
{{ $color->name }}`
to
到
{{ Form::checkbox('colors[]', $color->id, $checked) }}
{{ $color->name }}
Question 2: There is actually a perfect method given to you for this. Use
问题2:其实有一个完美的方法给你。用
$door->colors()->sync(Input::get('colors'));
It will both delete the old ones and add all the new ones in one shot.
它将删除旧的并一次性添加所有新的。
回答by windmaomao
Suppose you are modeling user and role and want to edit user with roles.
假设您正在为用户和角色建模,并且想要编辑具有角色的用户。
In your controller edit,
在您的控制器编辑中,
$user = User::find($id);
$roles = Role::lists('name', 'id'); // to populate all roles
In your template if you use select,
在您的模板中,如果您使用选择,
{{ Form::select('roles[]', $roles, array_pluck($user->roles, 'id'), ['multiple']) }}
In your controller update,
在您的控制器更新中,
$inputs = Input::all();
$roles = $inputs['roles'];
$user->roles()->sync($roles);
// $user->fill($inputs);
// $user->save();