我必须得到某一供应商为每一供应品提供的供应数量。我知道这听起来很奇怪,但让我解释一下:
我有一个供应商模式,其中有许多供应,每一个供应有许多库存,有一个数量。所以,现在我正在构建一个视图,在这个视图中,我想代表每个供应商的数量。
这就是我在控制器里想出来的:
foreach ($suppliers as $supplier) {
foreach($supplier->supplies as $supply){
if(!$comp_supplies->contains('name', $supply->name)){
$comp_supplies->push(['name'=>$supply->name, 'supplier'=>[['name'=> $supplier->name, 'quantity' => $supply->stocks->first()->quantity]]]);
}elseif($comp_supplies->contains('name', $supply->name)){
$array = (['name'=> $supplier->name, 'quantity' => $supply->stocks->first()->quantity]);
$array2 = $comp_supplies->where('name', $supply->name)->first()['supplier'];
array_push($array2, $array);
//dd($array2);
$comp_supplies->where('name', $supply->name)->first()['supplier'] = $array2;
dd($comp_supplies->where('name', $supply->name)->first()['supplier']);
}
}
}所以我在重复我的供应商,然后再重复一遍他们每个供应商的供应。现在,我想填补我想要的集合作为一个结果。
如果此集合不包含一个名称为“$ supplier > name”的供应项,则我将推送一个具有供应名称的数组,并创建一个数组“供应者”,在该数组中,我还将设置包含当前供应商信息的第一个条目。
现在我们已经接近我的问题了。
如果comp_supply已经包含具有当前供应名称的供应,则如果必须将新供应商推入我们在第一个" If“中创建的现有”供应商“数组中。
因此,我创建了$array,它保存了新的供应商信息,$array2,它保存了我们已经制作的供应者数组的$array2。
现在,如果我将$array推到$array2和dd(array2)上,一切都会按照我的意愿工作。但如果我现在
$comp_supplies->where('name', $supply->name)->first()['supplier'] = $array2 然后
dd($comp_supplies->where('name', $supply->name)->first()['supplier']);它并没有改变。
我在这个问题上被困了将近两个小时,而且真的很沮丧。
如果有人知道我能做些什么来解决这个问题,或者知道我下一步可以去哪里,请告诉我。
以下也是移民问题:
供应商:
public function up()
{
Schema::create('suppliers', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->unsignedInteger('coordinates_id')->index()->nullable();
$table->timestamps();
});
}用品:
public function up()
{
Schema::create('supplies', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->unsignedInteger('supplier_id');
$table->foreign('supplier_id')
->references('id')
->on('suppliers')
->onDelete('cascade');
$table->timestamps();
});
}库存:
public function up()
{
Schema::create('stocks', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('supplies_id');
$table->foreign('supplies_id')
->references('id')
->on('supplies')
->onDelete('cascade');
$table->integer('quantity');
$table->timestamps();
});
}发布于 2019-02-28 18:24:19
您的问题与PHP处理数组的方式有关。数组是通过值传递的,而不是通过引用传递的,因此,当调用$comp_supplies->where('name', $supply->name)->first()时,您实际上得到了数组的副本,因此,在索引'supplier'处修改值只会修改副本的值,而不会修改原始数组的值。
可以使用以下示例代码验证此行为:
class TestList
{
protected $values;
public function push($value)
{
$this->values[] = $value;
}
public function get($index)
{
return $this->values[$index];
}
}
$list = new TestList();
$list->push(['test' => 1]);
var_dump($list->get(0)['test']); // int(1)
$list->get(0)['test'] = 2;
var_dump($list->get(0)['test']); // still int(1)... 您可以通过使用对象而不是数组来克服这个问题,因为通过引用传递对象(注意(object)强制转换):
$list = new TestList();
$list->push((object)['test' => 1]);
var_dump($list->get(0)->test); // int(1)
$list->get(0)->test = 2;
var_dump($list->get(0)->test); // int(2)!发布于 2019-02-28 16:34:11
尝尝这个
$comp_supplies->where('name', $supply->name)->first()->update($array2)而不是
$comp_supplies->where('name', $supply->name)->first()['supplier'] = $array2 发布于 2019-02-28 18:30:55
而不是
$comp_supplies->where('name', $supply->name)->first()['supplier'] = $array2;你应该使用:
$key = $comp_supplies->where('name', $supply->name)->get()->keys()->first();
$comp_supplies[$key]['supplier'] = $array2;因为您推入集合数组,所以首先需要找到集合的有效键,所以以后可以更新该键的数组。
https://stackoverflow.com/questions/54929835
复制相似问题