DEV Community

Agus Sudarmanto
Agus Sudarmanto

Posted on • Edited on

Laravel Notes

From now on i will share my lesson notes when i learn Laravel Framework. Most notes i will write are commands and script modifications i did on my script, which might come in handy when I repeat the procedure again or you might want to use it later.

1. Start install Laravel

# Without Docker

composer create-project laravel/laravel example-app
cd example-app
php artisan serve
Enter fullscreen mode Exit fullscreen mode

# With Docker (using Laravel Sail)

curl -s "https://laravel.build/example-app" | bash

# -- OR -- install mysql, redis & devcontainer too
curl -s "https://laravel.build/example-app?with=mysql,redis&devcontainer" | bash

cd example-app
./vendor/bin/sail up
Enter fullscreen mode Exit fullscreen mode

2. Setup Laravel

With config file

I set my timezone and local in config/app.php file

With environment file

I set my site or database profile within .env file

3. Add Hot Reload for Laravel Development

3.1. Add directories to vite config /vite.config.js

import { defineConfig } from "vite";
// [1] add refreshPaths
import laravel, { refreshPaths } from "laravel-vite-plugin";
import tailwindcss from "@tailwindcss/vite";

export default defineConfig({
    plugins: [
        laravel({
            input: ["resources/css/app.css", "resources/js/app.js"],
            // [2] add directories
            refresh: [
                ...refreshPaths,
                "app/Livewire/**",
                "app/Filament/**",
                "app/Providers/**",
            ],
        }),
        tailwindcss(),
    ],
    server: {
        watch: {
            ignored: ["**/storage/framework/views/**"],
        },
    },
});
Enter fullscreen mode Exit fullscreen mode

3.2. Add hooks to boot /app/Providers/AppServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Spatie\Permission\PermissionRegistrar;
use App\Models\Permission;
use App\Models\Role;
// [1] Add libraries
use Filament\Support\Facades\FilamentView;
use Illuminate\Support\Facades\Blade;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        //
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        app(PermissionRegistrar::class)
            ->setPermissionClass(Permission::class)
            ->setRoleClass(Role::class);

        //-- [2] Add auto reload (hot reload)
        parent::register();
        FilamentView::registerRenderHook(
            "panels::body.end",
            fn(): string => Blade::render("@vite('resources/js/app.js')"),
        );
    }
}
Enter fullscreen mode Exit fullscreen mode

3.3. Run Vite in development mode pnpm run dev

Top comments (0)