我必须得到某一供应商为每一供应品提供的供应数量。我知道这听起来很奇怪,但让我解释一下:
我有一个供应商模式,其中有许多供应,每一个供应有许多库存,有一个数量。所以,现在我正在构建一个视图,在这个视图中,我想代表每个供应商的数量。
这就是我在控制器里想出来的:
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 16:34:11
尝尝这个
$comp_supplies->where('name', $supply->name)->first()->update($array2)而不是
$comp_supplies->where('name', $supply->name)->first()['supplier'] = $array2 https://stackoverflow.com/questions/54929835
复制相似问题