Alternative ways of doing the same things in Laravel

By Dillon Smart · · · 1 Comments

Laravel Alternative

Laravel is a large framework, with contributions coming from almost 2000 developers worldwide. Over its 10 year journey, there have been additions to the framework to do the same thing, only shorter. In this post, I have highlighted some alternative ways of doing the same things in Laravel.

Get the authenticated users id in Laravel

There are a number of different ways of retrieving the authenticated user’s id in Laravel. Below are some of the most common ones.

auth()->user()->id

or

\Auth::user()->id

or you can use the shorter way

auth()->id()

How to clear cache in Laravel

Laravel provides a few commands to clear cache files from the filesystem. The commands below each target a different cache:

php artisan cache:clear

php artisan config:clear

php artisan route:clear

But did you know you can clear all caches with one command!

php artisan optimize:clear

Route views in Laravel

I have seen this a number of times in projects I have either inherited or projects I have helped junior developers with. Routes that call a controller method, but only call a static view. There is a simpler way of doing this, and it’s been there since Laravel 5.5 which was released in 2017!

Route::view('/', 'front.pages.home.index')->name('website-home');

Do you know of an alternative way of doing something in Laravel? Share it below!

PHP isset function

This isn’t strictly related to Laravel. In PHP isset function determines if a variable is declared and different than null. The isset function supports multiple variables.

if(isset($var1) && isset($var2)) {
    // variables are declared
}

A cleaner solution would be:

if(isset($var1, $var2)) {
    // variables are declared
}

 

LaravelPHP

1 Comment

Laravel 9: How to Add a New Column to an Existing Table in a Migration - IKnowThatNow

[…] has a number of features designed to make developing web applications easier and faster. Migrations in Laravel allows us to […]

Was this helpful? Leave a comment!

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a custom WordPress taxonomy for posts and pages

Updated 16th August 2022

Sometimes when building a website using WordPress, the standard taxonomies Categories and Tags just aren’t enough, and you need to create more. Let’s learn how to create our own custom WordPress taxonomy. WordPress has made creating your own taxonomy easy, and you can even add your custom taxonomies to pages too. To accomplish this we

Laravel php artisan make:auth command not defined

Updated 11th August 2022

Laravel 6.0 LTS came with a lot of changes to the framework, apart from moving the the new Semantic Versioning, Laravels famous php artisan make:auth command was dropped. php artisan make:auth Before the Laravel make:auth command would create all the Migrations, Models, Controllers and Routes needed to easily setup an authenticatable application in seconds. However,

How to setup a remote MySQL database connection in PHPStorm

Updated 1st October 2023

PHPStorm is packed full of features designed to make the development process for PHP developers efficient and streamlined. Although Jetbrains have a standalone product for interacting with databases, named DataGrip, PHPStrom ships with the Database Tools and SQL plugin. This tools is powerful, integrated directly within the IDE, and provides a convinient way to intract