Sep 23, 2016
0
Level 4
Many to Many with additional field
Hey guys,
i have actual 3 tables:
- customers
- marketingoptions
- customer_marketingoption (pivot with - id | customer_id | marketingoption_id | status | created_at | updated_at)
My Marketingoption-Model
/**
* Marketingoptions belongsToMany Customers
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function customers()
{
return $this->belongsToMany(Customer::class)->withPivot('status')->withTimestamps();
}
My Customer-Model
/**
* Customers belongsToMany Marketingoptions
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function marketingoptions()
{
return $this->belongsToMany(Marketingoption::class)->withPivot('status')->withTimestamps();
}
My MarketingoptionsController
/**
* Edit Customer->Marketingoptions
*
* @param Customer $customer
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function edit(Customer $customer)
{
$marketingoptions = Marketingoption::pluck('marketingoption', 'id', 'status');
return view('marketingoptions.edit', compact('customer', 'marketingoptions'));
}
/**
* Update Customer->Marketingoptions
*
* @param Request $request
* @param $id
* @return \Illuminate\Http\RedirectResponse
*/
public function update(Request $request, $id)
{
$customer = Customer::findOrFail($id);
$marketingoptionsIds = $request->input('marketingoptions');
$customer->marketingoptions()->sync($marketingoptionsIds);
return back();
}
and last but not least my Form
<div class="form-actions">
<div class="row">
<div class="col-md-12">
<select name="marketingoptions[]" multiple="multiple" id="tags">
@foreach($marketingoptions as $key => $value)
<option value="{{ $key }}">{{ $value }}</option>
@endforeach
</select>
</div>
</div>
</div>
@foreach($customer->marketingoptions as $option)
<p>{{ $option->marketingoption }} - {!! $option->value_clear !!}</p>
@endforeach
<!-- Add Article Form Input -->
<div class="form-actions">
<div class="row">
<div class="col-md-offset-2 col-md-10">
<button data-loading-text="Bitte warten..." class="submit-loading-btn btn blue"
type="submit">{{ $submitButtonText }}</button>
</div>
</div>
</div>
This work all fine now, i can use the multiselect to sync my options with customers in the pivot-table... but i want to switch this select to Radiobuttons grouped by Marketingkoptions to define the status for each option. How can i get the value from the radiobuttons and write it into the pivot (additional) field?
Thanks im realy stuck with that.
Please or to participate in this conversation.