In what might be the final post on learning Laravel with this being the week 3 blog post.
The outcome being I released my first project made with Laravel which was actually just a rebuild of My idlers (Originally Vanilla PHP).
The project can be found here. It is CRUD based with authorization (Laravel Breeze).
Now for the thoughts and findings during week 3:
No command for database creation
For Laravel there is no single, one-line command to create the database. Given Migrations handles the structure and creation for tables creating the actual database should too be a simple Artisan command.
Instead, you have to build up a small custom command to create the database or connect and run a query on the database using the command line or a GUI program.
Sessions
Setting and getting sessions is done in a simple manner alike to Vanilla PHP:
Session::put('session_name', $value); Session::get('session_name');
Getting all of the session data:
Session::all();
Checking if session contains a value:
Session::has('session_name');
Forgetting a value or flushing all of the session data:
Session::forget('key'); Session::flush();
Gitignore
Thanks to gitignore.io getting the base git .ignore file for a Laravel and PHPStorm project was easy. The direct link is here for the two combined.
This helped to ensure only the essential files were committed to GitHub and all the unnecessary files were not.
Blade @yield and @section
Yield and section in blade files allows for injecting into layouts on certain pages
app.blade.php
<html> <head> .... @yield('style') </head> ....
Then in the layout child
<x-app-layout> @section('style') <style> body: { color: #7b6f6f; } </style> @endsection ........
This will put the CSS styling in the header of the child page. Doing it this way is for when you want styling or scripts on only certain pages and not the whole inheritance of app layout.
Sitemap generator
Laravel sitemap is a package to generate a sitemap without having to manually enter URLs. Its usage can be just 2 lines:
use Spatie\Sitemap\SitemapGenerator; SitemapGenerator::create('https://mysite.com')->writeToFile('sitemap.xml');
Custom error pages
Custom error pages can be used by creating a blade file in resources/views/errors/
with the status code being the file name e.g 404.blade.php
Symfony\Component\HttpKernel\Exception\HttpException
gets passed in as $exception
.
<h2>{{ $exception->getMessage() }}</h2>