我得到了未定义的偏移量:1尝试导出到csv时

时间:2019-04-19 07:55:11

标签: laravel-5.5

我试图用此代码导出客户,

    $users = DB::table('users')
        ->select('users.id', 'users.name as customer', 'users.email', 'users.phone_number', 'plans.name as plan')
        ->join('subscriptions', 'subscriptions.user_id', '=', 'users.id')
        ->join('plans', 'plans.id', '=', 'subscriptions.plan_id')
        ->where('subscriptions.status', 'active')
        ->get();

    $fp = fopen('php://memory', 'r+');

    fputcsv($fp, [
        'ID',
        'Last Name',
        'First Name',
        'Email',
        'Phone',
        'Plan',
    ]);

    foreach ($users as $key => $user) {
        if (!empty($user->customer)) {
            list($first_name, $last_name) = explode(' ', $user->customer);

            $data = [
                $user->id,
                $last_name,
                $first_name,
                $user->email,
                $user->phone_number,
                $user->plan
            ];

            fputcsv($fp, $data);
        }
    }

    rewind($fp);
    $csv_line = stream_get_contents($fp);
    file_put_contents('customer1.csv', rtrim($csv_line) . "\n", FILE_APPEND);
    fclose($fp);

但是当我访问网址时,

我收到此错误

Undefined offset: 1

在这一行list($first_name, $last_name) = explode(' ', $user->customer);

但是对于本地开发人员来说,没问题,那是什么意思,我不知道自己在哪里。

0 个答案:

没有答案