Laravel Form Model Binding&可以更新多对多口才关系选框 [英] Many-to-Many Eloquent relationship update with Laravel Form Model Binding & Checkboxes

查看:82
本文介绍了Laravel Form Model Binding&可以更新多对多口才关系选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3张桌子:

  • id
  • 名称
  • 图片

颜色

  • id
  • 名称
  • 图片

door_colors

  • id
  • door_id
  • color_id

和2种具有多对多关系的模型(每个门具有多种颜色,许多颜色在门到门之间重叠):

and 2 models with a many-to-many relationship (each door comes in a variety colors, and many colors overlap door-to-door):

门型号

class Door extends Eloquent {
    public function colors()
    {
        return $this->belongsToMany('Color', 'door_colors');
    }
}

颜色模型

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);
    }
}

管理门表单视图

{{ Form::model($door) }}
Colors:
@foreach ($colors as $color)
{{ Form::checkbox('colors[]', $color->id) }} {{ $color->name }}
@endforeach
{{ Form::close() }}

问题1:如何做到这一点,以便在输出复选框时,选中与当前门存在关系的复选框,而取消选中与当前门有关系的复选框.

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.

问题2::一旦选中相应框并点击提交",我将如何更新关系? $door->colors()->detach();清除该门所有现有的,然后$door->colors()->attach($color_id_array);根据颜色ID的数组创建新的?

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?

感谢任何输入!

推荐答案

问题1:您应该将其传递到包含表单的视图中,尽管它也可以在视图中使用,但这并不是最佳实践.做类似的事情...

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...

$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;

然后您将更改

{{ Form::checkbox('colors[]', $color->id) }} 
{{ $color->name }}` 

{{ Form::checkbox('colors[]', $color->id, $checked) }}
{{ $color->name }}

问题2 :实际上,您可以为此找到一种完美的方法.使用

Question 2: There is actually a perfect method given to you for this. Use

$door->colors()->sync(Input::get('colors'));

它将同时删除旧文件并将所有新文件添加到一张照片中.

It will both delete the old ones and add all the new ones in one shot.

这篇关于Laravel Form Model Binding&可以更新多对多口才关系选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆