Laravel - 未定义的索引

时间:2016-09-07 09:18:01

标签: php laravel laravel-5

我想要做的是同时添加费用和供应商,因为知道费用表和供应商表之间的关系是一对多的关系。 供应商可以拥有vendor_contacts,这是一个与供应商表具有一对多关系的表。

这是我的代码:

a.flat_map { |e| e[0].is_a?(Array) ? e : [e] }
#=> [["a", "b", "c"], ["d", "e", "f"], ["g", "h", "i"], ["l", "m", "n"]]

这是我们想要保存供应商时要执行的代码:

if (isset($data['vendor'])) {
            $canSaveVendor = false;
            $vendorPublicId = array_get($data, 'vendor.public_id') ?: array_get($data, 'vendor.id');
            if (empty($vendorPublicId) || $vendorPublicId == '-1') {
                $canSaveVendor = Auth::user()->can('create', ENTITY_VENDOR);
            } else {
                $vendor = Vendor::scope($vendorPublicId)->first();
            }
            if ($canSaveVendor) {
                $vendor = $this->vendorRepo->save($data['vendor']);
            }
            if ($canSaveVendor) {
                $data['vendor_id'] = $vendor->id;
            }
        }
        $expense = $this->expenseRepo->save($data, $expense);
        $vendor = $expense->vendor;
        $vendor->load('vendor_contacts');

        return $expense; 

当我尝试在供应商表中正确保存组织的供应商时,但费用都不是vendor_contacts 它给了我if ($vendor) { // do nothing } elseif (!$publicId || $publicId == '-1') { $vendor = Vendor::createNew(); } else { $vendor = Vendor::scope($publicId)->with('vendor_contacts')->firstOrFail(); \Log::warning('Entity not set in vendor repo save'); } $vendor->fill($data); $vendor->save(); $first = true; $vendorcontacts = isset($data['vendor_contact']) ? [$data['vendor_contact']] : $data['vendor_contacts']; foreach ($vendorcontacts as $vendorcontact) { $vendorcontact = $vendor->addVendorContact($vendorcontact, $first); $first = false; } 我该怎么办?

1 个答案:

答案 0 :(得分:1)

你在这里弄错了:

$vendorcontacts = isset($data['vendor_contact']) 
    ? [$data['vendor_contact']] 
    : $data['vendor_contacts'];

尝试这样:

$vendorcontacts = isset($data['vendor_contact']) 
    ? [$data['vendor_contact']] 
    : [];