“Class 'App\Http\Controllers\InvoicesExport' not found”in Maatwebsite Laravel

6xfqseft  于 2023-04-22  发布在  其他
关注(0)|答案(3)|浏览(138)

在我的Laravel项目中,我使用Maatwebsite\Excel以Excel格式导出数据。我使用新版本3.0(Maatwebsite\Excel)

App/Exports.php

namespace App\Exports;
use App\Purchasepaymenttransaction;

use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;

class InvoicesExport implements FromView
{
    public function view(): View
    {
            $purchasepayment=Purchasepaymenttransaction::Where('transaction_category',2)->OrderBy('transaction_date','DESC')->get();

        return view('exports.purchasepayments', [
            'purchasepayment' => $purchasepayment
        ]);
    }
}

在我的付款控制器中

<?php

namespace App\Http\Controllers;
use DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use App\Purchasepaymenttransaction;
use Excel;
use App\Exports;

class PaymentController extends Controller
{

    public function purchaseexport()
        {

            return Excel::download(new InvoicesExport, 'invoices.xlsx');
        }

}

我得到“类'App\Http\Controllers\InvoicesExport'未找到”错误

kq0g1dla

kq0g1dla1#

return Excel::download(new \InvoicesExport, 'invoices.xlsx');

通常情况下,当你遇到class not found异常时,你可以在class前使用反斜杠('')。它会自动从你的代码中找到那个类,并在你需要的地方使用它,而不需要在文件的顶部使用它。它类似于

use [YOUR_PATH]/InvoicesExport

如果你使用“use”语句,那么就不需要反斜杠('')。你可以使用以上两种中的任何一种。它都会起作用。

sshcrbum

sshcrbum2#

使用App\Exports\InvoicesExport作为ExportsInvoicesExport;

5vf7fwbs

5vf7fwbs3#

将付款控制器的上部更新为

namespace App\Http\Controllers;
use DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use App\Purchasepaymenttransaction;
use App\Exports\InvoicesExport;
use Maatwebsite\Excel\Facades\Excel;

然后就完成了。另外,将InvoicesExport文件中的行从use App\Purchasepaymenttransaction;更新为use App\Models\Purchasepaymenttransaction;

相关问题