Laravel收藏问题

时间:2019-02-28 16:08:02

标签: php laravel laravel-5 array-push laravel-collection

经过大量的糊涂和搜索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”的供应,我将推送一个具有供应名称的数组,并创建一个数组“ suppliers”,在其中我还将当前供应商设置为第一个条目信息。

现在我们正在接近我的问题。

如果comp_supply已经包含具有当前供应名称的供应,则必须将新供应商推入我们在第一个“ if”中创建的现有“供应商”阵列中。

因此,我创建了$ array和$ array2,其中$ array保存了新的供应商信息,而$ array2保存了供应商的数组()['供应商'])。

现在,如果我将$ array推到$ array2和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();
    });
}

3 个答案:

答案 0 :(得分:1)

您的问题与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)!

答案 1 :(得分:0)

尝试一下

def get_context_data(self, **kwargs):
    inicio = self.kwargs.get('data_inicio')
    fim = self.kwargs.get('data_fim')
    return dict(
        super(RelatorioDetail, self).get_context_data(**kwargs),
        fluxo_list = Fluxo.objects.filter(data__range=[inicio, fim])
    )

代替

$comp_supplies->where('name', $supply->name)->first()->update($array2)

答案 2 :(得分:0)

代替

$comp_supplies->where('name', $supply->name)->first()['supplier'] = $array2;

您应该使用:

$key = $comp_supplies->where('name', $supply->name)->get()->keys()->first();
$comp_supplies[$key]['supplier'] = $array2;

因为您要推送到集合数组中,所以首先您需要找到集合的有效键,以便稍后可以为此键更新此数组