Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rajcoder's avatar

Class not found error

Im using maatwebsite/excel package so according to its doc i created a class eg. PostsExport.php in app/Exports folder.

namespace Demo\Exports;

use Demo\Post;

class PostsExport implements FromCollection
{
    public function collection()
    {
        return Post::all();
    }
}

And in my PostController i have the method

use Demo\Exports\PostsExport;
use Maatwebsite\Excel\Facades\Excel;

...
...

public function export()
{
    return Excel::download(new PostsExport, 'posts.xlsx');
}

But when i click download button the export method runs but gives error that Demo\Exports\PostsExport not found

0 likes
3 replies
click's avatar

Namespaces should match directory names

Examples:

Path: app/Exports/PostsExport.php
namespace: App\Exports
filename: PostsExport.php
class: PostsExport

Path: app/Http/Controllers/Api/SomeApiController.php
namespace: App\Http\Controllers\Api
filename: SomeApiController.php
class: SomeApiController

Your namespacing is wrong. You use Demo but you should have a namespace App\Exports

rajcoder's avatar

I found the solution.

I had simply forgotten to include the word php after <?

rbcunhadesign's avatar

Try running composer dump-autoload and see if anything happens. If not you can work around it and register the package on the config/app.php file.

Please or to participate in this conversation.