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…

1 year ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

1 year ago

FTP getting array of file details such as size using PHP

Using FTP and PHP to get an array of file details such as size and…

2 years ago

Creating Laravel form requests

Creating and using Laravel form requests to create cleaner code, separation and reusability for your…

2 years ago

Improving the default Laravel login and register views

Improving the default Laravel login and register views in such a simple manner but making…

2 years ago

Laravel validation for checking if value exists in the database

Laravel validation for checking if a field value exists in the database. The validation rule…

2 years ago