为什么Excel中的文本字段在Laravel中作为数字导入?

问题描述 投票:0回答:1

我正在尝试将Excel工作表导入Laravel Nova,但是Excel中的文本字段被导入为数字。将数据导入Excel中的原样是不正确的(文本=文本,数字=数字)。

Excel:screenshot

Laravel Nova的结果:screenshot

链接转到屏幕截图,无法显示嵌入式图像

我使用https://laravel-excel.com/https://novapackages.com/packages/anaseqal/nova-import

ContactsImport.php

<?php

namespace App\Imports;

use App\Contact;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;

class ContactsImport implements ToCollection
{

public function collection(Collection $rows)
{
    foreach ($rows as $row) {
        Contact::create([
        'first_name' => $row[0],
        'last_name' => $row[1],
        'address' => $row[2],
        'postal_code' => $row[3],
        'city' => $row[4],
        'country' => $row[5],
        'cellphone' => $row[6],
        'email' => $row[7],
        ]);
    }
}

}

ImportContacts.php

namespace App\Nova\Actions;

use Illuminate\Bus\Queueable;
use Anaseqal\NovaImport\Actions\Action;
use Illuminate\Support\Collection;
use Laravel\Nova\Fields\ActionFields;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

use Laravel\Nova\Fields\File;

use App\Imports\ContactsImport;
use Maatwebsite\Excel\Facades\Excel;

class ImportContacts extends Action
{
use InteractsWithQueue, Queueable, SerializesModels;

/**
 * Indicates if this action is only available on the resource detail view.
 *
 * @var bool
 */
public $onlyOnIndex = true;

/**
 * Get the displayable name of the action.
 *
 * @return string
 */
 public function name() {
    return __('Import Contacts');
 }

/**
 * @return string
 */
public function uriKey() :string
{
    return 'import-contacts';
}

/**
 * Perform the action.
 *
 * @param  \Laravel\Nova\Fields\ActionFields  $fields
 * @param  \Illuminate\Support\Collection  $collections
 * @return mixed
 */
public function handle(ActionFields $fields, Collection $collections)
{
    Excel::import(new ContactsImport, $fields->file);
    return Action::message('The contacts have been imported.');
}

/**
 * Get the fields available on the action.
 *
 * @return array
 */
public function fields()
{
    return [
        File::make('File')
            ->rules('required'),
    ];
}
}

我已经尝试了几个小时的类似搜索,但找不到任何东西。

laravel laravel-nova laravel-excel
1个回答
0
投票

[通过阅读此docs确保通过名称键获取列,然后再次尝试var_dump($rows)以确保从excel文件中获取正确的值。

© www.soinside.com 2019 - 2024. All rights reserved.