Categories: Development

First project; Learning Laravel week 3

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

Documentation.

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

Documentation.

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>
Share

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

2 years ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

2 years ago

Early September spring shots

Some early September spring shots from a bushwalk, It is still too early for wildflowers.

3 years ago

Cloning an NVMe to SK Hynix Platinum P41 NVMe

I recently needed to clone over a smaller NVMe to a 2TB SK Hynix Platinum…

3 years ago

Using Laravel’s deleteDirectory to delete folders and their contents

If you ever needed a simple solution in Laravel to delete an entire directory and…

3 years ago

How to allow only 1 user to register in a Laravel application

How to enforce only one user to register or be registered in a Laravel web…

3 years ago