如何从 check-boxes 收集值并将该值同步到数据透视表 table?

How to gather value(s) from check-boxes, and sync that value to a pivot table?

我想从复选框中获取值,并将这些值同步到我的数据透视表 table。

我有 3 tables :

这是我试过的方法

查看 > 我的 check-boxes

    {{ Form::label('export_frequency' , 'Export Frequency', array('class'=> 'required cool-blue'))}} <br>

    @foreach (ExportFrequency::all() as $export_frequency)

    <input type="checkbox" name="{{$export_frequency->name}}" id="{{$export_frequency->id}}" value="{{$export_frequency->name}}">
    {{$export_frequency->name}} <br>

    @endforeach

在我的控制器中(CatalogDownloadController.php)

public function store()
    {

        $catalog_download        = new CatalogDownload;
        $catalog_download->title = Input::get('title');
        $catalog_download->save();

        foreach(ExportFrequency::all() as $export_frequency ){

            $export_frequency_id = Input::get($export_frequency->name);

            if(is_array($export_frequency_id))
            {
                $catalog_download->export_frequencies()->sync([$export_frequency_id, $catalog_download_id]);
                $catalog_download_id = $catalog_download->id;
            }
        }

        return Redirect::to('catalog_downloads/')
        ->with('success','The catalog_download was created succesfully!');

}

目标

同样,我只想同步:$export_frequency_id、$catalog_download_id 到我的 catalog_download_export_frequency table。

问题

谁能告诉我我错过了什么?结果不会同步。 请随时给我suggestions/advice。 感谢您的宝贵时间。

这应该做到,伙计:

// Your form view
{{ Form::label('export_frequencies' , 'Export Frequencies', array('class'=> 'required cool-blue'))}} <br />
@foreach ($exportFrequencies as $exportFrequency)
    <input type="checkbox" name="export_frequencies[]" id="{{ $exportFrequency->id }}" value="{{ $exportFrequency->id }}">
    {{ $exportFrequency->name }}<br />
@endforeach

// Your store method
public function store()
{
    $input = Input::except('export_frequencies');
    $exportFrequencies = Input::get('export_frequencies'); // Use get, not only

    $catalogDownload = $this->catalogDownload->create($input); // Using dependency injection here, so don't forget to assign your CatalogDownload model to $this->catalogDownload in your contructor

    if (isset($exportFrequencies)
    {
        $catalogDownload->exportFrequencies()->attach($exportFrequencies);
    }

    return Redirect::to('catalog-downloads')->with('success', 'The Catalog Download was created successfully!');
}