Laravel Random ressources

This post contains a list of random useful resources, regarding some pretty common Laravel solutions. Consider it as a kind of shortcut to a lot of googling.

Laravel 5.7 from scratch

Best “getting started” guide if you are into video tutorials.

The “max key length is 767 bytes” problem

As described here, but in brief, you should open the file app/Provider/AppServiceProvider.php and add code to the boot() method as bellow

use Illuminate\Support\Facades\Schema;

/**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Schema::defaultStringLength(191);
    }

Since the tables are actually created when you try to perform the migration without the above code, you should drop the database and recreate it before retrying to run the migration again (or at least drop all tables). Failing to do so will result in one more error instructing you that the table ‘users’ already exists.

Console output methods

From the manual

To send output to the console, use the lineinfocommentquestion and error

Laravel Documentation

Many to many relatioship using eloquent

Try this excellent guide.

However, there is no need to use MyISAM storage engine because it is not universally available and will be less and less. Omitting this line $table->engine = 'MyISAM'; is perfectly OK.

Also note an important thing: foreign keys must be of the same data type than primary keys or you will get the foreign key constraint is incorrectly formed error. This can easily happen to beginners because when you create a migration with artisan, the data type of the primary key will be biginteger while you might be used to use integer data type for this purpose. Pay attention to that!

Pivot table names in alphabetical order

What the above article does not mention is an important detail in the naming convention of the pivot table.

Name of the pivot table should consist of singular names of both tables, separated by underscore symbol and these names should be arranged in alphabetical order, so we have to have product_shop, not shop_product

Laravel daily

Seeder throw error Class does not exist

When you write a new seeder, chances are it will throw an error

Seeding: UsersTableSeeder

   ReflectionException  : Class UsersTableSeeder does not exist

Just keep calm and dump your autoloader.

composer dump-autoload

For details see this stackoverflow question

Laravel behind Cloudflare and https

If you are running your Laravel application behind Cloudflare, using https, chances are, you will have mixed content problems. Laravel has sorted out this problem a long ago. All you need to do is set the trusted proxies in the TrustProxies middleware as described in the manual. This is all you have to do because the TrustProxies middleware is enabled by default on all routes. Note that this will work for all reverse proxies, load balancers ant the like.