经过很多googeling并搜索stackoverflow后我决定需要你的帮助。
我必须得到某个供应商的每个供应的供应数量。我知道这听起来很奇怪,但让我解释一下:
我有一个供应商模型,有很多供应,每个供应都有很多有数量的库存。所以现在我建立一个视图,我希望用某个供应商的数量来表示每个供应。
这就是我在控制器中提出的:
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']);
}
}
}
所以我迭代我的供应商,并再次迭代他们每个人的供应。现在我想填充我想要的收藏品。
如果此集合不包含名称为“$ supply-> name”的供应,我将使用supply-name推送一个数组并创建一个数组“供应商”,其中我还设置了第一个带有当前供应商信息的条目。
现在我们正在接近我的问题。
如果comp_supply已经包含具有当前供应名称的供应,则必须将新供应商推入我们在第一个“if”中创建的现有“供应商”数组中。
因此我创建了$ array,它包含新的供应商信息,$ array2,它包含供应商数组($ comp_supplies-> where('name',$ supply-> name) - > first()['supplier' ])我们已经做了。
现在,如果我将$ array推入$ array 2和dd(array2),一切都按照我想要的方式工作。但如果我现在设定
$comp_supplies->where('name', $supply->name)->first()['supplier'] = $array2
然后
dd($comp_supplies->where('name', $supply->name)->first()['supplier']);
它并没有改变。
我坚持这个问题将近2小时,真的很沮丧。
如果有人知道我可以做些什么来解决这个问题,或者知道我接下来要去哪看,请告诉我。
以下是迁移:
供应商:
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();
});
}
您的问题与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)!
试试这个
$comp_supplies->where('name', $supply->name)->first()->update($array2)
代替
$comp_supplies->where('name', $supply->name)->first()['supplier'] = $array2
代替
$comp_supplies->where('name', $supply->name)->first()['supplier'] = $array2;
你应该使用:
$key = $comp_supplies->where('name', $supply->name)->get()->keys()->first();
$comp_supplies[$key]['supplier'] = $array2;
因为你进入集合数组,所以首先你需要找到有效的集合密钥,以便稍后你可以为这个密钥更新这个数组