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.

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

Find php.ini on Apple Silicon installed with Homebrew

Updated 1st October 2023

Are you having trouble finding your php.ini file which was installed locally with Homebrew? Packages installed with Homebrew can be installed in different locations making it hard to find, especially if you are on new Apple Mac running Apple’s Silicon chips. Here, I will show you home to locate your locally installed php.ini file installed

Laravel 419 page expired after login [SOLVED]

Updated 1st January 2024

Laravel is the most popular PHP framework, dominating the space since 2014, and the chosen framework by many new PHP developers. Many new developers run into the same errors when first using the framework, the most common being a 419 Page Expired error on a form post.  What is Cross-Site Request Forgery (CSRF)? Cross-site request