Laravel is How Development Should Be

Laravel is How Development Should Be

Read 44 times

#php

#laravel

#programming

I have not been doing that much writing. Not because I’ve nothing to write about, no. It’s because I have been head down, silent and learning.

I have been learning Laravel. If you read this article of mine you will recall I said I wouldn’t reach for Laravel because I already use TYPO3. I had to walk back that statement when I realised while I have TYPO3, it is better suited to more involved projects.

What changed? Well, I have a biggish project I am working on and I landed in a situation where SvelteKit wasn’t sufficient and TYPO3 was too much. I needed a midpoint - and I was not going to go back to Nextjs.


Laravel

Instead of trying to rattle my head with something new, I chose to leverage the PHP that I already know. On another journey I went, and what a wondrous one it turned out to be.

Why Laravel Works?

It just does. In encounters that I have had with it, it shows a lot of thought went into creation of the framework. I don’t know how to explain it, but it just does. Most things you may want to do, Laravel most likely has an in-built solution. Batteries included, I hear people say.

Authentication and Authorisation

A thing that we all have to handle as developers is auth. In previous times I have always left this until the end because a number of the options were a headache to deal with.

This is not the case with Laravel. You could choose to setup up the authentication flow on your own with code that sort of looks like this:

public function store()
    {
        //validate
        $validatedAttributes = request()->validate([
            'first_name' => ['required'],
            'last_name' => ['required'],
            'email' => ['required', 'email'],
            'password' => ['required', Password::min(6), 'confirmed'],
        ]);

        // create user
        $user = User::create($validatedAttributes);

        // log in
        Auth::login($user);

        //redirect
        return redirect('/dashboard');


    }

Or you could pick on of the starters that handle this for you. Some of them adding a few features to the process.

The same goes for authorisation. Based on how you want the access to things to be be you can specify the authorisation rules on the specific routes, app-wide or on individual controllers.

// auth on a route
Route::get('/jobs/{job}/edit', [JobController::class, 'edit'])
    ->middleware('auth')
    ->can('edit', 'job');

// the policy that manages it
 public function edit(User $user, Job $job): bool
    {
        return $job->employer->user->is($user);
    }

Database Relations

A thing that has given me trouble when building is database relations. I recall a time on one of the projects I was working on I got stuck trying to define database relations with Prisma. Some of it was because of how new I was at it. But even after I understood what was required the particular process manged to confuse me more.

Laravel on the other hand is intuitive about relations. Yes, you have a couple of places you have to ensure you have handled. But that, I have found makes you consider things more deeply and pay close attention.

Take for example; you have an Employers table and a Jobs table. Each Employer can have many Jobs. For that you would:

Image description


Code snippets in this article are taken from my Learn Laravel in 30 days repository as well as the final project from that.


Appreciation

I would also like to thank Jeffrey Way for making my journey into Laravel an enjoyable experience. I highly recommend the 30 days to learn Laravel playlist.


What About SvelteKit?

It’s a very unfortunate thing really. I find myself in the position of having to walk back on things I said.

A little while after I adopted Svelte, several changes were made that go against the simplicity I have been aiming for. As such, if I am to use SvelteKit now, it would be in the state that I found it in when I started and now. As for the time being, me and Sveltekit are taking a break.


Wrapping Up

I never paid that much attention to Laravel and I regret that. However, at least I have seen the light.

Although, I am not a fan of how the Laravel team are trying to streamline the project creation process. I don’t like starters I didn’t make is all.

I have been doing a lot of walking back my statements recently, huh?


Thank you for reading, let’s connect!

Thank you for visiting this little corner of mine. Let’s connect on Twitter, Discord and LinkedIn

Back to articles