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.

Laravel Undefined Constant header_x_forwarded_all after upgrading Laravel version

Updated 1st January 2024

Upgrading to a newer version of Laravel can be exciting, but it can also be nerve racking if you are unfamiliar with the changes in newer version, especially if your upgrading multiple times. Have you received an “Undefined Constant” error in Laravel after upgrading? In this post, I will help you understand the changes under

How to use Microsoft Graph API with Laravel

Updated 1st January 2024

Microsoft Graph is a gateway to data and intelligence within Microsoft 365. Microsoft Graph is great to build apps for organizations and consumers using the vast amount of data stored in Microsoft 365. In this post, we are going to integrate with Microsoft Graph API in Laravel to get all users assigned to your tenant.

Pixel Tracking in PHP

Updated 16th August 2022

When creating email campaigns, it’s important to know how many recipients actually opened your email. This information can help you determine if your efforts are working or if it’s time to change your strategy. Most mail champaign tools such as Mailchimp and SendInBlue use a technique called Pixel Tracking. In this post, you will learn