Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel-将附加列值保存到透视表_Php_Laravel_Orm_Pivot Table - Fatal编程技术网

Php Laravel-将附加列值保存到透视表

Php Laravel-将附加列值保存到透视表,php,laravel,orm,pivot-table,Php,Laravel,Orm,Pivot Table,我想将其他布尔值保存到透视表中的中间表。 我有一些与相关的模型。我想像这样保存pivot table城市学校 但所有学校都保存了相同的值。对于每个学校id,只能存储相关的is\u open值 city_id | school_id | is_open 1 1 0 1 2 1 我的模型: City.php public function schools() { return $this->belongs

我想将其他布尔值保存到透视表中的中间表。 我有一些与
相关的模型。我想像这样保存pivot table城市学校

但所有学校都保存了相同的
值。对于每个学校id,只能存储相关的
is\u open

city_id | school_id | is_open
   1          1         0
   1          2         1
我的模型:

City.php

public function schools()
{
    return $this->belongsToMany(School::class)->withPivot('is_open')->withTimestamps();
}
public function cities()
{
    return $this->belongsToMany(City::class)->withPivot('is_open')->withTimestamps();
}
<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
School.php

public function schools()
{
    return $this->belongsToMany(School::class)->withPivot('is_open')->withTimestamps();
}
public function cities()
{
    return $this->belongsToMany(City::class)->withPivot('is_open')->withTimestamps();
}
<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
View.blade.php

public function schools()
{
    return $this->belongsToMany(School::class)->withPivot('is_open')->withTimestamps();
}
public function cities()
{
    return $this->belongsToMany(City::class)->withPivot('is_open')->withTimestamps();
}
<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

由于
$schools
是一个数组,因此您必须循环并构造所谓的“syncArray”:

在上面的示例中,
$syncArray
将包含一个
$schoolIds
数组,映射到一个“附加属性”数组,在这种情况下,
是开放的

[1 => ["is_open" => 1], 2 => ["is_open" => 0]]
然后,您只需调用:

$data->schools()->syncWithoutDetaching($syncArray);

数据透视表中的所有记录都将被更新,以反映所传递的内容。通常,您会调用
sync()
,但这会删除
$syncArray()
中不包含的任何内容,而且由于这只是更新属性,您不会希望这样做。

在控制器中:
$school
是学校的id吗?一定要这样。顺便问一下:一所学校怎么可能位于多个城市?它不应该是一对多关系吗?
$request->has('is_open')
总是
true
,因为从技术上讲它是一个数组。我认为您需要执行
$request->has(“是开放的。{$school}”)?1:0
获取特定学校的状态。事实上,如果
$request->input('school')
也是一个数组,那么你必须将它循环到
$request->has(“is_open.{$school}”)
才能工作…@Maartenverman我本想对学校/城市说同样的话,但这是一个术语;我知道在多个城市都有同一所“学校”,但这是因为它有多个“校园”@TimLewis Changing
$request->has(“是开放的。{$school}”)?1:0
给出错误
数组到字符串的转换
@Maarten,是的,
$school
id运行良好,而logic for cities/school是同一所学校在不同城市的分支机构。@是的,请参阅我的第二个注释;我没有看到
$school
是一个ID数组。我想我有办法,让我试试看。