Igor Simic
3 years ago

Laravel - problems after fresh installations and how to solve them

So, you just installed latest version of Laravel (in my case it was Laravel 7.x) and you are so happy and excited. You typed "php artisan serve" or maybe even before that  php artisan migrate  and... boom.... excitement is gone... you are getting bunch of errors. Cold shower..... 
Here is what you can do about it... explanation and how to fix some of them

 Unable to prepare route [api/user] for serialization. Uses Closure.

Laravel by default creates two routes in routes/api.php and routes/web.php  both of them are using closure and this is causing problems. For the quick fix you can comment them out:

//Route::middleware('auth:api')->get('/user', function (Request $request) {
//    return $request->user();

//Route::get('/', function () {
//    return view('welcome');

Ok, this bug is fixed, let's run again php artisan migrate...

oh no, another error:

Argument 1 passed to Illuminate\Database\Connectors\MySqlConnector::setModes() must be an instance of PDO, instance of Doctrine\DBAL\Driver\PDOConnection given

OK, this one could be my mistake, i have included  
"doctrine/dbal": "3.0.x-dev",
package to my project - but is not needed for my version of Laravel. Delete doctrine/dbal from composer.json and run
composer update
to remove this package.

Nice, two errors gone, let's try again
php artisan  migrate

aaaand.... boom! 
Another error

 Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users` add unique `users_email_unique`(`email`))

WTF is this?
Syntax error on fresh installed Laravel? How the fook is this possible?

Boot, is not actually Syntax error, it is DB issue. Apparently for those using older version of MariaDB or MYSQL this issue can occur. 
OK, so let's fix this one as well. Open AppServiceProvider from app/providers/ and update boot method:
use Illuminate\Support\Facades\Schema;
public function boot()
    Schema::defaultStringLength(191); // add this line

Let's try again.
php artisan migrate

aaaand.... again


  SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists

OK, this should be easy one - just delete the freaking table. This is our first installation anyway. So, go to DB and delete users table, it will be triggered by next make migration anyway.

drop users table

php artisan migrate

it is working this time. Tables are created!.... can't believe it.... fook yeah.... i am the man... my work for today is over.
see you tomorrow you fooks :)

Laravel succes installation is done!