Laravel's Eloquent:使用Eloquent多项同步多项

时间:2016-10-05 11:58:30

标签: php model eloquent relationship

我在同步这两个模型时遇到问题......制造商可能属于多个部分......问题是当我这样做时:

    $manufacturer = Solidstav\Models\Manufacturer::find($_POST['manufacturer_id']);

    if (!empty($filename)) {
        $manufacturer->logo = $filename;
    }

    foreach ($languages as $lang) {
        $name = $lang . '_name';
        $desc = $lang . '_description';

        $manufacturer->{$name} = $_POST[$name];
        $manufacturer->{$desc} = $_POST[$desc];
    }

    $saved = $manufacturer->save();
    $manufacturer->sections()->sync($_POST['sections']); // [2, 3]

在这种情况下,部分是表格中的复选框。

它只保存数组中的第一个值。所以上面的例子只保存" 2"我被重定向回数据来自的页面......

// Manufacturer.php
public function sections()
{
    return $this->belongsToMany('Solidstav\\Models\\Section');
}

// Section.php
public function manufacturer()
{
    return $this->belongsToMany('Solidstav\\Models\\Manufacturer');
}

这在我之前从未发生过......

0 个答案:

没有答案