Home > Back-end >  Laravel Export : Function name must be a string
Laravel Export : Function name must be a string

Time:04-20

Having a problem with this code

<?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'
        ]);
    }
}

it says on line 21 which is "return SubmissionDetail::where('submission_id', $this->id)->get()(["

CodePudding user response:

You have error in your code. You typed your get() wrong. The get() method should receive parameters you want, not try to be executed as a function:

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

EDIT:

You can also just select fields you want from the start, without getting all the fields first, and then filtering them down:

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