I want to save additional boolean value to to intermediate table in pivot table.
I have models with belongstoMany relation. I want to save pivot table city_school like this.
But the same value of is_open saved for all schools. Only related is_open value must be stored for each school_id.
city_id | school_id | is_open
1 1 0
1 2 1
My Models:
City.php
public function schools()
{
return $this->belongsToMany(School::class)->withPivot('is_open')->withTimestamps();
}
School.php
public function cities()
{
return $this->belongsToMany(City::class)->withPivot('is_open')->withTimestamps();
}
View.blade.php
<div>LA<input type="hidden" name="school[]" value="1">
<input type="checkbox" name="is_open[1]">Open
<div>SF<input type="hidden" name="school[]" value="2">
<input type="checkbox" name="is_open[2]">Open
Controller
$school = $request['school'];
$data->schools()->attach($school, ['is_open' => $request->has('is_open')?1:0]);