I have 2 distinct tables and a pivot table :
sectors
id
admin_id
created_at
updated_at
deleted_at
langs
id
langname_fr
langname
....
lang_sector
lang_id
sector_id
sectname
sectshortname
....
As i want to manage multilingual content, I use fields in the pivot table for translations.
In order to display the form for editing the values I created a method :
public function edit($id)
{
$sectors = Sector::with('langs')->where('id','=', $id)->get();
return view('admin.pages.maps.edit', compact('sectors', 'id'));
}
This method returns a view with a form that allows user to create several entries depending the number of languages which are defined.
{!! Form::open( array('route' => 'maps.store','method' => 'POST') ) !!}
<fieldset>
<legend>Sector name</legend>
@foreach($langs as $lang)
<div class="form-group m-form__group">
{{ Form::label( 'Sector name in ' . $lang->langname_en) }}
{{ Form::text('sectname_lang_' . $lang->id, '' , [ 'class' => 'form-control m-input' ]) }}
</div>
<div class="form-group m-form__group">
{{ Form::label( 'Sector short name in ' . $lang->langname_en ) }}
{{ Form::text('sectshortname_lang_' . $lang->id, '', [ 'class' => 'form-control m-input' ]) }}
</div>
@endforeach
</fieldset>
...
{!! Form::close() !!}
If i want to create an entry in my database, i have to create several entries ... I have been reading official documentation here https://laravel.com/docs/5.5/eloquent-relationships#updating-many-to-many-relationships
At this level I have to collect values in the HTML form and make an update.
So I created a method :
public function update(Request $request, $id)
{
$sector = Sector::findOrFail($id);
$countLang = Lang::count();
for ($i = 1; $i <= $countLang; $i++) {
$insertSector[$i] = $sector->langs()->syncWithoutDetaching(
$sector->id, // sector_id
$i, // 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', 'Secteur mis à jour');
}
It returns this :
{"1":{"attached":[],"detached":[],"updated":[]},"2":{"attached":[],"detached":[],"updated":[]}}
In fact i notice that i have 2 issues ...
This works if i don't delete any language ... For example i could use language with ID 1 and 2 or languages with ID 3 and 4 ...
In this case it wouldn't work.
My main issue is to know how to update fields which are in the pivot table ... I don't have to change the keys...
For example i have to change the pivot table
lang_id sector_id sectname sectshortname
-------------------------------------------------------------------------------------------------
1 1 originalname lang1 originalshortname lang1
2 1 originalname lang2 originalshortname lang2
....
Onto :
lang_id sector_id sectname sectshortname
-------------------------------------------------------------------------------------------------
1 1 new name lang1 new shortname lang1
2 1 new name lang2 new shortname lang2
....
Thanks for your help