Laravel 导出:函数名称必须是字符串

Laravel Export : Function name must be a string

此代码有问题

<?php

namespace App\Exports;

use App\SubmissionDetail;
use Maatwebsite\Excel\Concerns\FromCollection;

class SubmissionDetailExport implements FromCollection
{
    protected $id;

    function __construct($id) {
            $this->id = $id;
    }
    
    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        return SubmissionDetail::where('submission_id', $this->id)->get()([
            'submission_id', 'nama_barang', 'image_path', 'jumlah', 'harga_satuan', 'harga_total', 'keterangan'
        ]);
    }
}

它在第 21 行说是“return SubmissionDetail::where('submission_id', $this->id)->get()(["

您的代码有错误。您输入的 get() 有误。 get() 方法应该接收你想要的参数,而不是试图作为一个函数来执行:

return SubmissionDetail::where('submission_id', $this->id)->get([
            'submission_id', 'nama_barang', 'image_path', 'jumlah', 'harga_satuan', 'harga_total', 'keterangan'
        ]);

编辑:

您也可以从一开始就只 select 个您想要的字段,而不是先获取所有字段,然后再过滤它们:

return SubmissionDetail::select('submission_id', 'nama_barang', 'image_path', 'jumlah', 'harga_satuan', 'harga_total', 'keterangan')->where('submission_id', $this->id)->get();