使用雄辩的Laravel未将数据插入Mysql

时间:2019-06-14 03:55:20

标签: laravel eloquent

我在使用Larvel中的雄辩模型 insert()方法将数据插入数据库表时遇到问题。但是当我单击“提交”按钮时,数据未插入数据库中。

这是我实现的代码:-

控制器

public function receivedAll(Request $request, ItemPR $item_code)
{

    $item_code      = $request->item_code;
    $pr_qty         = $request->pr_qty;
    $uploadFile     = $request->file('file_document');

    $update_data    = ItemPR::whereIn('item_code', explode(",", $item_code))->update(['receive'=> 'received']);

    $get_data       = ItemPR::whereIn('item_code', explode(",", $item_code))->orWhereIn('pr_qty', explode(",", $pr_qty))->get();

    $data = [];
    foreach($get_data as $value) {

        if(is_array($uploadFile)) {

            foreach($uploadFile as $file) {

                $filename = $file->getClientOriginalName();
                $folder[] = $file->storeAs('uploads', $filename);  
            }

            $data[] = 
            [
                'item_code'      => $value->item_code,
                'qty'            => $value->pr_qty,
                'file_document'  => $filename,
                'created_at'     => Carbon::now(),
                'created_by'     => Auth::user()->nick_name,
                'last_update'    => Carbon::now(),
                'last_update_by' => Auth::user()->nick_name,
            ];
        }

        WarehouseInventory::insert($data);
    }

    // Model elequent insert

    return response()->json(['success'=>"Products Updated successfully."]);
}

型号:-WarehouseInventory.php

class WarehouseInventory extends Model
{
    protected $table = 'warehouse_inventori';
    protected $primaryKey = 'pr_item';

    public $incrementing = false;

    const CREATED_AT = 'created_at';
    const UPDATED_AT = 'last_update';

    protected $fillable = [
        'pr_item', 'item_code', 'qty', 'po_number', 'warehouse_id', 'file_document', 'created_at', 'last_update', 'last_update_by', 'created_by'
    ];
}

任何想法或我的代码有什么问题吗?

1 个答案:

答案 0 :(得分:0)

您正在雄辩地使用insert()方法,insert()是DB类的方法,而Eloquent则使用create()方法,

WarehouseInventory::create($data);

src:https://laravel.com/docs/5.8/eloquent#inserting-and-updating-models