无法在Excel :: load()之外获取对象名称值

时间:2018-12-06 11:36:00

标签: php laravel maatwebsite-excel

我正在创建联系人目录。我有一个包含联系人号码的联系人组。我正在使用这个作曲家软件包"maatwebsite/excel": "~2.1.0"。我可以将联系人组保存在contact_groups表中,但是我的代码无法获取新创建的联系人组的ID。

这是我的代码。

$group = new ContactGroup();
$group->company_id = Auth::user()->company_id;
$group->group_name = $request['group_name'];
     if ($group->save()) {
        Excel::load($request->file('file')->getRealPath(), function ($reader) {
            foreach ($reader->toArray() as $key => $row) {
               $contact = new Contact();
               $contact->group_id = $group->id;
               $contact->company_id = Auth::user()->company_id;
               $contact->contact_name = $row['contact_name'];
               $contact->contact_number = $row['contact_number'];
               $contact->save();
             }
        });
        Session::flash('success','New contact group has been created!');
        return back();
     }

我遇到一个错误,说Undefined variable: group

,它指向此行$contact->group_id = $group->id;

谢谢大家!

2 个答案:

答案 0 :(得分:2)

您必须使用$group关键字在匿名函数内传递use

function ($reader) use ($group) {

Read more

答案 1 :(得分:1)

@media all and (min-width: 800px) { /* 12 background repetitions */ div { background-size: calc(100% / 12) auto } } @media all and (min-width: 1200px) { /* 18 background repetitions */ div { background-size: calc(100% / 18) auto } } 条件内更改您的代码,使其看起来像以下内容:

function openNav() {
  document.getElementById('myNav').style.height = '70px';
  document.getElementById('myNav').style.opacity = '100%';
}

function closeNav() {
  document.getElementById('myNav').style.height = '0%';
  document.getElementById('myNav').style.opacity = '0%';
}
if (screen.width <= 643) {
    function openNav() {
  document.getElementById('myNav').style.height = '100%';
  document.getElementById('myNav').style.opacity = '100%';
}

function closeNav() {
  document.getElementById('myNav').style.height = '0%';
  document.getElementById('myNav').style.opacity = '0%';
}
}