Laravel Excel 导入报错

之前都是能导入的,隔了两天没用就出问题了,代码一直没动过,有大神帮忙看看是怎么回事吗?

《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 3

这报错信息不是已经很明显了吗?

意外的传参类型,预期是 model ,你传了一个 string,90% 原因是传参错误。

2年前 评论

我没用过这个,如果确定你没修改且之前能用,可以看看是不是更新了扩展包 :see_no_evil:

2年前 评论

这报错信息不是已经很明显了吗?

意外的传参类型,预期是 model ,你传了一个 string,90% 原因是传参错误。

2年前 评论
随波逐流

扩展是 maatwebsite/excel: ^3.1, 使用 Collection 导入的. :flushed:

<?php

namespace App\Imports;

use App\Models\Competition;
use App\Models\CompetitionGroup;
use App\Models\CompetitionGroupRanking;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;

class CompetitionGroupRankingImport implements ToCollection
{
    public $competition;
    public $competitionGroup;

    public function __construct(Competition $competition, CompetitionGroup $competitionGroup)
    {
        $this->competition = $competition;
        $this->competitionGroup = $competitionGroup;
    }

    /**
     * @inheritDoc
     */
    public function collection(Collection $collection)
    {
        $collect = collect();
        foreach ($collection as $item) {
            if (isset($item[0]) && (is_numeric($item[0]) || $item[0] == null) && isset($item[1]) && $item[1]) {
                $collect->push([
                    'competition_id'       => $this->competition->id,
                    'competition_group_id' => $this->competitionGroup->id,
                    'ranking'              => $item[0],
                    'name'                 => $item[1],
                    'score'                => $item[2],
                ]);
            }
        }
        CompetitionGroupRanking::query()->where('competition_id', '=', $this->competition->id)->where('competition_group_id', '=', $this->competitionGroup->id)->delete();
        CompetitionGroupRanking::query()->insert($collect->toArray());
    }
}
<?php

namespace App\Http\Controller;

class CompetitionController extends Controller{
    /**
     * 导入赛事成绩
     *
     * @param  Request  $request
     *
     * @return JsonResponse
     */
    public function groupRankingImport(Request $request)
    {
        try {
            $validated = $this->validate($request, [
                'competition_id'       => ['required', 'exists:competitions,id'],
                'competition_group_id' => ['required', 'exists:competition_groups,id'],
                'file'                 => ['required', 'file', 'mimes:xlsx,xls'],
            ]);
            $competition = Competition::query()->find($validated['competition_id']);
            $competition_group = CompetitionGroup::query()->find($validated['competition_group_id']);
            Excel::import(new CompetitionGroupRankingImport($competition, $competition_group), $validated['file']);
            return $this->success();
        } catch (ValidationException $e) {
            return $this->fail($e->validator->errors()->first());
        }
    }
}
2年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!