更新数据透视表中的字段 table Laravel 5.5

Update fields in a pivot table Laravel 5.5

我有 3 个表(2 个 + 数据透视表):

categories
    id
    admin_id
    created_at
    updated_at
    deleted_at

langs
    id
    langname_fr
    langname
    ....

lang_sector
    lang_id
    sector_id
    sectname
    sectshortname
    ....

我创建了一个表单,允许根据我定义的语言数量创建多个条目...

{!! Form::open( array('route' => 'maps.store','method' => 'POST') ) !!}
<fieldset>
    <legend>Nom du secteur</legend>

    @foreach($langs as $lang)

        <div class="form-group m-form__group">
            {{ Form::label( 'Nom du secteur en ' . $lang->langname_fr) }}
            {{ Form::text('sectname_lang_' . $lang->id, '' , [ 'class' => 'form-control m-input' ]) }}
        </div>
        <div class="form-group m-form__group">
            {{ Form::label( 'Nom abrégé du secteur en ' . $lang->langname_fr ) }}
            {{ Form::text('sectshortname_lang_' . $lang->id, '', [ 'class' => 'form-control m-input' ]) }}
        </div>
    @endforeach
</fieldset>
...

{!! Form::close() !!}

如果我想在我的数据库中创建一个条目,我必须创建多个条目...

public function sectorCreate(Request $request) {

    Sector::create(array(
        'admin_id' => Auth::guard('admin')->user()->id,
        'created_at' => Carbon::now(),
        'updated_at' => Carbon::now(),
     ));

    $sector = Sector::all()->last();
    $sector_id = Sector::all()->last()->id;
    $countLang = Lang::count();

    for ($i = 1; $i <= $countLang; $i++) {
        $insertSector[$i] = $sector->langs()->attach(
            $sector_id,
            [
                'lang_id' => $i,
                'sectname' => $request->input('sectname_lang_' .$i),
                'sectname_slug' => Str::slug($request->input('sectname_lang_' .$i)),
                'sectshortname' => $request->input('sectshortname_lang_' .$i),
                'sectdescription' => $request->input('sectdescription_lang_' .$i),
                'sectshortdescription' => $request->input('sectshortdescription_lang_' .$i),
                'created_at' => Carbon::now(),
                'updated_at' => Carbon::now(),
                'deleted_at' => NULL
            ]
        );
    }
    return redirect()->route('admin.home')->with('success', 'Secteur créé');
}

现在我的问题是知道如何更新数据库的值和删除条目...我试图阅读文档,但我不确定我是否理解它。

例如

lang_id     sector_id      sectname      sectshortname 
-------------------------------------------------------
1          1           longname1      shortname1
2          1           longname2      shortname2

更新后我想更新 sectname 和 sectshortname ...我已经使用 sync、syncWithoutDetaching 和 updateExistingPivot 进行了多次尝试但没有成功...

我还通过考虑 lang_id 和 sector_id 作为主键来添加约束 ...

更新------------------------------------ ------------------

我使用 sync 和 syncWithoutDetaching 修改了更新方法

public function update(Request $request, $id)
{
    $sector = Sector::findOrFail($id);
    $countLang = Lang::count();

    $langs = Lang::all();
    foreach ($langs as $lang) {
        $lang_id = $lang->id;
    }

    for ($i = 1; $i <= $countLang; $i++) {
        $insertSector[$i] = $sector->langs()->sync(
            $sector->id,
            $lang_id,
            [
                'sectname' => $request->input('sectname_lang_' .$i),
                'sectname_slug' => Str::slug($request->input('sectname_lang_' .$i)),
                'sectshortname' => $request->input('sectshortname_lang_' .$i),
                'sectdescription' => $request->input('sectdescription_lang_' .$i),
                'sectshortdescription' => $request->input('sectshortdescription_lang_' .$i),
                'updated_at' => Carbon::now(),
                'deleted_at' => NULL
            ]
        );
    }
    return $insertSector;
    //return redirect()->route('maps.index')->with('success', 'updated');
}

Documentation 声明如下:

将关系附加到模型时,您还可以传递一组附加数据以插入中间 table:

$user->roles()->attach($roleId, ['expires' => $expires]);

这部分你答对了。现在更新(和/或删除):

正在删除

$user->roles()->detach([1, 2, 3]);

这将删除关联记录并清除中间 table。

同步关联

你也可以使用sync方法来构造多对多关联。 sync 方法接受一组 ID 以放置在中间 table 上。任何不在给定数组中的 ID 将从中间 table 中删除。所以,这个操作完成后,中间只会存在给定数组中的ID table:

$user->roles()->sync([1, 2, 3]);

您还可以通过以下 ID 传递额外的中间 table 值:

$user->roles()->sync([1 => ['expires' => true], 2, 3]);

如果您不想分离现有 ID,可以使用 syncWithoutDetaching 方法:

$user->roles()->syncWithoutDetaching([1, 2, 3]);

结论

使用sync,重新设置属性。如果您只想更新几条记录,请使用 syncWithoutDetaching.

更新

将您的更新代码更改为:

$insertSector[$i] = $sector->langs()->sync(
    $lang_id => 
    [
        'sectname' => $request->input('sectname_lang_' .$i),
        'sectname_slug' => Str::slug($request->input('sectname_lang_' .$i)),
        'sectshortname' => $request->input('sectshortname_lang_' .$i),
        'sectdescription' => $request->input('sectdescription_lang_' .$i),
        'sectshortdescription' => $request->input('sectshortdescription_lang_' .$i),
        'updated_at' => Carbon::now(),
        'deleted_at' => NULL
    ]
);

您通过了 sector->id$lang_id,您只需要通过具有中间 table.

属性的 $lang_id

最后感谢 Douwe de Haan,我终于解决了使用数据透视表创建条目的问题 table ...我想我现在有点了解它是如何工作的

方法如下:

public function store(Request $request)
{
    Sector::create(array(
        'admin_id' => Auth::guard('admin')->user()->id,
        'created_at' => Carbon::now(),
        'updated_at' => Carbon::now(),
    ));

    $sector = Sector::all()->last();
    $countLang = Lang::count();
    $langs = Lang::all();

    foreach ($langs as $lang) {
        $lang_id[] = $lang->id;
    }
    for ($i=0 ; $i < $countLang; $i++) {
        $insertSector[$i] = $sector->langs()->syncWithoutDetaching(
            [$lang_id[$i] =>
                [
                    'sectname' => $request->input('sectname_lang_' .$lang_id[$i]),
                    'sectname_slug' => Str::slug($request->input('sectname_lang_' .$lang_id[$i])),
                    'sectshortname' => $request->input('sectshortname_lang_' .$lang_id[$i]),
                    'sectdescription' => $request->input('sectdescription_lang_' .$lang_id[$i]),
                    'sectshortdescription' => $request->input('sectshortdescription_lang_' .$lang_id[$i]),
                    'created_at' => Carbon::now(),
                    'updated_at' => Carbon::now(),
                    'deleted_at' => NULL
                ]
            ]
        );
    }
    return redirect()->route('maps.index')->with('success', 'Secteur créé');
}

待更新:

public function update(Request $request, $id)
{
    $sector = Sector::findOrFail($id);
    $countLang = Lang::count();

    $langs = Lang::all();
    foreach ($langs as $lang) {
        $lang_id[] = $lang->id;
    }

    for ($i=0 ; $i < $countLang; $i++) {
        $insertSector[$i] = $sector->langs()->updateExistingPivot(
            $lang_id[$i],
                [
                    'sector_id' => $request->input('sector_id'),
                    'sectname' => $request->input('sectname_lang_' .$lang_id[$i]),
                    'sectname_slug' => Str::slug($request->input('sectname_lang_' .$lang_id[$i])),
                    'sectshortname' => $request->input('sectshortname_lang_' .$lang_id[$i]),
                    'sectdescription' => $request->input('sectdescription_lang_' .$lang_id[$i]),
                    'sectshortdescription' => $request->input('sectshortdescription_lang_' .$lang_id[$i]),
                    'updated_at' => Carbon::now(),
                    'deleted_at' => NULL
                ]

        );
    }
    return $insertSector;
    //return redirect()->route('sectors.index')->with('success', 'Secteur mis à jour');
}