【问题标题】:(Laravel) How to total the integer column and send to the another table?(Laravel)如何汇总整数列并发送到另一个表?
【发布时间】:2019-05-04 20:01:11
【问题描述】:

如何将feeds表中具有相同cycle_id、type和user_id的数量相加,并将数量、cycle_id、type和user_id之和传递给inventories表?

feeds

    $table->increments('id');
    $table->date('date_input');
    $table->string('delivery_number');  
    $table->string('type');
    $table->integer('quantity');
    $table->unsignedInteger('cycle_id');  
    $table->unsignedInteger('user_id');
    $table->timestamps();

inventories

 $table->increments('id');
            $table->string('type');
            $table->integer('overall_quantity');
            $table->unsignedInteger('cycle_id');  
            $table->unsignedInteger('user_id');
            $table->timestamps();

数量之和将传递给整体数量。
feeds 表中的类型将传递给 inventories 表中的类型。
feeds 表中的cycle_id 将传递给inventories 表中的cycle_id。
feeds 表中的 user_id 将传递给 inventories 表中的 user_id。

如果库存表中不存在该条目,它将创建,但当 inventories 表中存在该条目时,它将添加。

这是我的代码

FeedController.php

  public function store(Request $request)
    {

               //validate
               $this->validate($request, array(
                'date_input' => 'required|date',
                'delivery_number' => 'required|numeric',
                'type' => 'required|max:255',
                'quantity' => 'required|numeric'
             ) );
             $input= Carbon::parse($request->get('date_input'));
             $cycle = Cycle::where('date_of_loading','<=',$input)
             ->where('date_of_harvest','>=',$input)
             ->first();

             $cycle_id =$cycle->id ?? 0;

        $feed = Feed::create([
            'date_input' => request('date_input'),
            'delivery_number' => request('delivery_number'),
            'type' => request('type'),
            'quantity' => request('quantity'),
            'cycle_id'     => $cycle_id,
           'user_id'     => Auth::id()

        ]);

        return $feed;

        $overall_quantity = Feed::where('cycle_id','=',$cycle_id)
                            ->where('type','=',$request->get('type'))
                            ->sum('quantity');

        Inventory::firstOrCreate([
            'overall_quantity' => $overall_quantity, 
            'type' => request('type'), 
            'cycle_id' => $cycle_id,
            ]);
    }

但是没用 当我在feeds 表中添加数据时,inventories 表仍然是空的。


新问题

我的提要历史记录

我的inventories

它应该是 id 1 将是 144 但它会创建新条目。请帮助

【问题讨论】:

    标签: php database laravel eloquent laravel-query-builder


    【解决方案1】:

    在运行Inventory::firstOrCreate([...]) 命令之前,您返回了$feed

    只需删除 return 语句即可。

    // return $feed;
    
    $overall_quantity = Feed::where('cycle_id','=',$cycle_id)
                        ->where('type','=',$request->get('type'))
                        ->sum('quantity');
    
    Inventory::firstOrCreate([    
    //Add unique field combo to match here
     'type' => request('type'), 
     'cycle_id' => $cycle_id,
    ],[ 'overall_quantity' => $overall_quantity ]);
    

    【讨论】:

    • 我有新问题
    猜你喜欢
    • 2019-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-28
    • 2021-07-24
    • 1970-01-01
    • 2018-10-29
    相关资源
    最近更新 更多