81

I moved my project from desk to another.
When I run php artisan it does not work.

I tried to run composer update, but it returns the error

Script @php artisan package:discover handling the post-autoload-dump event returned with error code 255

zx485
  • 28,498
  • 28
  • 50
  • 59
Ghyath Darwish
  • 2,614
  • 4
  • 15
  • 31
  • 2
    For me it was caused by folder permissions. chown -R u:g, and chmod -R 755 did the job for me. – Barry Jun 04 '19 at 17:32
  • it was an issue in a trait but i couldnt find no matter what, using something like https://packagist.org/packages/nunomaduro/larastan can catch that – ctf0 Feb 04 '21 at 16:18
  • I tried all the soultion and nothing worked I am running laravel 4.2.10 and php 7 @Ghyath Darwish – user123456 Feb 26 '22 at 21:27
  • This could happen for n number of reasons hence any or all the of the answers work for any of us. To know the exact reason check your PHP error log file. This will tell you exactly where the issue is. – SkoolCodeian Oct 24 '22 at 23:34

28 Answers28

149

This is how I solved this after an upgrade from laravel version 6.x - 7.x:

In App\Exceptions\Handler changed

//Use Exception;
Use Throwable;

Then methods to accept instances of Throwable instead of Exceptions as follows:

//public function report(Exception$exception);
public function report(Throwable $exception);

//public function render($request, Exception $exception);
public function render($request, Throwable $exception);

In config\session.php:

//'secure' => env('SESSION_SECURE_COOKIE', false),
'secure' => env('SESSION_SECURE_COOKIE', null),

Then run composer update

m02ph3u5
  • 3,022
  • 7
  • 38
  • 51
Oginga Michael
  • 1,491
  • 1
  • 7
  • 2
33

I solved the problem this way:

cd bootstrap/cache/
rm -rf *.php

The bootstrap directory contains the app.php file that initializes the structure. This directory also houses a cache directory that contains structure-generated files for performance optimization, such as files and route cache services. Laravel stores configuration files, provider, and cached services to optimize the fetching of this information. The problem with me was when the other developer ran the 'php artisan config: cache' command on your machine and since the cache folder contains files that can be deleted, I deleted them and solved the problem.

Siamak Motlagh
  • 5,028
  • 7
  • 41
  • 65
Samuel Terra
  • 683
  • 7
  • 5
  • It would help to explain what that is and why did it solve the problem – Javier Larroulet Dec 07 '18 at 17:17
  • The bootstrap directory contains the app.php file that initializes the structure. This directory also houses a cache directory that contains structure-generated files for performance optimization, such as files and route cache services. Laravel stores configuration files, provider, and cached services to optimize the fetching of this information. The problem with me was when the other developer ran the 'php artisan config: cache' command on your machine and since the cache folder contains files that can be deleted, I deleted them and solved the problem. – Samuel Terra Dec 08 '18 at 19:28
  • 18
    DON'T RUN THIS! This is really dangerous command written as it is! For example, if he's not in the project root directory, `cd` will fail, but `rm` command is going to execute! Run this either as `rm -rf bootstrap/cache/*` or `cd bootstrap/cache && rm -rf *php` – М.Б. Apr 24 '21 at 18:38
26

If this happened after Laravel update from 6.x to 7.x, then this could be due to the update of Symfony. See the upgrade guide of this part: https://laravel.com/docs/7.x/upgrade#symfony-5-related-upgrades

Arm092
  • 580
  • 7
  • 12
26

I was upgrading my Laravel from 5.8 to 8.0 and I got this error.

So my fixes were

  1. As @nobuhiroharada mentioned that I had missed .env file in my project

  2. Second is that Laravel removed Exception and replaced it with Throwable. So we need to fix that in our app\Exceptions\Handler.php. One can refer Medium.com for the error fix.

  3. In the upgrade guide of Laravel 8.x you need to update the dependencies like this

  4. Next, in your composer.json file, remove classmap block from the autoload section and add the new namespaced class directory mappings:

"autoload": {
    "psr-4": {
        "App\\": "app/",
        "Database\\Factories\\": "database/factories/",
        "Database\\Seeders\\": "database/seeders/"
    }
},
  1. Finally from bootstrap\cache delete the cache files and run composer update.

These 5 steps might help you remove the error you are facing in your Laravel Project.

dqureshiumar
  • 810
  • 1
  • 7
  • 19
19

This happens because you have upgraded to Laravel 7.

To fix it, update app/Exceptions/Handler.php like so:

<?php

namespace App\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable; // <-- ADD THIS

class Handler extends ExceptionHandler
{
    public function report(Throwable $exception) // <-- USE Throwable HERE
    {
        parent::report($exception);
    }
    public function render($request, Throwable $exception) // AND HERE
    {
        return parent::render($request, $exception);
    }
}

This is documented in the official upgrade guide here:

https://laravel.com/docs/7.x/upgrade#symfony-5-related-upgrades

doncadavona
  • 7,162
  • 9
  • 41
  • 54
GigaTera
  • 782
  • 8
  • 9
10

I got the same problem in Win 8 and solve it:

Here is the steps.

Step-1: Go to your project directory

Step-2: And type command cd bootstrap/cache/

Step-3: Again type command del -rf *.php

Step-4: Update your composer composer update

Step-5: Now you are done: php artisan serve

Thanks.

Y. Joy Ch. Singha
  • 3,056
  • 24
  • 26
8

Do you have .env file in your new project?

I had same error message. When I add .env file, error is gone.

success message like this.

Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover
Discovered Package: fideloper/proxy
Discovered Package: ixudra/curl
Discovered Package: laravel/tinker
Discovered Package: nesbot/carbon
Discovered Package: socialiteproviders/manager
Package manifest generated successfully.

I hope this will help you.

nobuhiroharada
  • 499
  • 4
  • 13
3

maybe you have an error in the project code (for example, in routes or controller). This may be one of the reasons for this error.

In my project, the web.php file has a syntax error. I defined this when I started the php artisan command

C:\OSPanel\domains\lara.shop.loc>php artisan
In web.php line 
  syntax error, unexpected end of file  
Sanya
  • 95
  • 1
  • 7
2

Same issue when I update laravel from 6.x to 7.x

I tried the most voted answer but it didn't work, then I used php artisan serve I noticed that:

RuntimeException

In order to use the Auth::routes() method, please install the laravel/ui package.

Try composer require laravel/ui maybe it will work.

李晓琳
  • 21
  • 3
2

I solve this error by deleting the vendor table then run composer update. I'm using Laravel 7. So, if you are not updating from the older Laravel version, maybe this is the solution.

ajico
  • 21
  • 1
1

I had this same problem when running composer update in a Laravel project. In the package.json it's configured to run artisan package:discover, which failed with:

Class 'Symfony\Component\Translation\Translator' not found in vendor/nesbot/carbon/src/Carbon/Translator.php on line 18

When I looked in the vendor/symfony/translation directory I found that it was completely empty, which explained the error.

The solution was to completely delete the vendor directory and then re-run composer update. This was the only way that I was able to make composer install the missing files.

leo
  • 1,175
  • 12
  • 13
1

Nothing worked, so I installed a new project, and I read Handler.php in App\Exceptions, it was different, probably because I copied some solution and Internet and deleted the following:

protected $dontReport = [
    //
];

protected $dontFlash = [
    'password',
    'password_confirmation',
];

I copy here all of Handler.php generated by laravel 7.5, may be useful for someone:

<?php

namespace App\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

/**
 * A list of the inputs that are never flashed for validation exceptions.
 *
 * @var array
 */
protected $dontFlash = [
    'password',
    'password_confirmation',
];

/**
 * Report or log an exception.
 *
 * @param  \Throwable  $exception
 * @return void
 *
 * @throws \Exception
 */
public function report(Throwable $exception)
{
    parent::report($exception);
}

/**
 * Render an exception into an HTTP response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Throwable  $exception
 * @return \Symfony\Component\HttpFoundation\Response
 *
 * @throws \Throwable
 */
public function render($request, Throwable $exception)
{
    return parent::render($request, $exception);
}
}
cigien
  • 57,834
  • 11
  • 73
  • 112
Federico
  • 33
  • 1
  • 5
1

I deleted composer.lock file and ran composer update.

That solved mine

ekpono
  • 172
  • 4
1

I had the same issue, my problem was the PHP version of the server account did not match my Docker container. The SSH terminal was using the global php version for the server.

php -v

Confirm it's the version your project needs.


Composer did warn me that a higher php version was required but I rm -rf'd /vendor and ./composer.lock without paying too much attention to the warnings!

Josh Bonnick
  • 2,281
  • 1
  • 10
  • 21
1

Got the same problem.

  1. php artisan doesn't work.

  2. composer install got error:

Script @php artisan package:discover handling the post-autoload-dump event returned with error code 255

And this works for me.

When I switch another linux user. It works. some files are owned by another linux user. So I use root account and change all the project file to the specific user,

chown -R www:www project/

and use that user to execute composer cmd

and then it works.

TsaiKoga
  • 12,914
  • 2
  • 19
  • 28
1

My case/solution, in case it helps anyone...

I copied my repo over from my old Windows computer to a new one, and installed the latest php.

composer install was returning:

Root composer.json requires php ^7.1.3 but your php version (8.1.10) does not satisfy that requirement

...which I thought was odd (assuming 8 satisfied ^7), so I continued on with composer install --ignore-platform-reqs, and ended up with this particular issue.

After trying a bunch of other possible solutions, what ended up working for me was simply downgrading to the same PHP version from my old machine (7.4.33).

Lys777
  • 426
  • 1
  • 3
  • 11
0

This is not an actual error. If you look a bit above you'll see the actual error. In my case, there was an error in my code:

PHP Fatal error:  Declaration of 
App\Exceptions\Handler::render($request, App\Exceptions\Exception $exception)
must be compatible with 
Illuminate\Foundation\Exceptions\Handler::render($request, Throwable $e)

It is not possible to tell you what is actually a problem in your code, so you have to look real reason for this error in your stack trace.

Axbor Axrorov
  • 2,720
  • 2
  • 17
  • 35
0

I deleted my project I created a new folder and cloned the repository again and after that I gave composer install / update.

cigien
  • 57,834
  • 11
  • 73
  • 112
0

In my case there is missing folder and its file Kernel.php in

app/Console

So I created app/Console/Kernel.php using code from previous project. Now everything working fine.

0

Make sure your config\constants.php (and/or resources\lang\en\local.php) has no syntax errors. I get this error a lot by missing commas in constants.php file.

Akshay K Nair
  • 1,107
  • 16
  • 29
0

I got the same problem in Win 10 and solve it:

Here is the steps.

Step-1: Go to your project directory

Step-2: Update your composer

composer update

Step-3: Now you are done: php artisan serve

cigien
  • 57,834
  • 11
  • 73
  • 112
Omkar Ghurye
  • 195
  • 1
  • 8
0

If you have this error the simplest way is you can try using composer install instead of composer update

cigien
  • 57,834
  • 11
  • 73
  • 112
0

For me it was related to Kernel.php

I was adding new schedule task into the Kernel. I also updated some controllers, views, and installed twilio extension.

The error did not provide more information than

Script @php artisan package:discover handling the post-autoload-dump event returned with error code 255

@Suresh Pangeni refences the Kernel.php doc so I checked by doc that is in PROJECTFOLDER\app\Console\Kernel.php

protected $commands = [
    Commands\Inspire::class,
    Commands\Test::class
    \App\Console\Commands\Message::class,
];

Missing Comma between Commands\Test::class and the next line didn't let me proceed. It provided no further warning or information when I ran composer dump-autoload.

Hope this can help someone else that has a similar issue!

0

I had the same error today, the causes are as follows: cause 1: the env file contains space in one of the configuration.

cause 2: incorrect configuration of the Handler file belonging to the App\Exceptions namespace;

cause 3: incorrect configuration of a file inheriting ExceptionHandler

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 10 '22 at 05:41
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/33123312) – David Ansermot Nov 11 '22 at 09:39
0

I was using Laravel 9.x and got the same error after trying to install this package maatwebsite/excel!

thanks to @samuel-terra and @dqureshiumar there is the solution worked for me:

  1. clear bootstrap/cache:
cd bootstrap/cache/
rm -rf *.php
  1. then run composer update:
composer update
mjavadtatari
  • 51
  • 2
  • 9
0

My problem was __construct method.

composer.json

"php": "^8.1",
"laravel/framework": "^9.19",

Handler.php

The problem originates from this code:

this->container->make(FlasherInterface::class);

My solution, I removed the construction directly and the problem is solved.

public function __construct(Container $container)
{
    parent::__construct($container);
    $this->flasher = $this->container->make(FlasherInterface::class);
    //$this->request = $this->container->get(Request::class);
}
Murat Çakmak
  • 151
  • 2
  • 7
0

Error is connected with command: php artisan package:discover

So in first step remove this command from composer.json

In second step ensure, that error is visible by adding on the top of /artisan file:

ini_set('display_errors', true); 
ini_set('log_errors', true);
ini_set('error_log', '/app/storage/logs/php.log');

Then manually run php artisan and see what a problem is, it would be many, for instance: Class 'Memcache' not found

-4

Getting this error when my composer version 2.x then i rollback this

composer self-update --1

Now its perfectly working

Mizanur Rahman Khan
  • 1,612
  • 1
  • 12
  • 19