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.

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.

How to check Composer version – Install and use Composer for PHP

Updated 11th January 2024

Composer is a dependency manager for PHP. First released in 2012, Composer, and its adoption by popular frameworks such as Laravel has single-handedly driven the rise in PHP adoption in recent years. In this post, I will show you how to install and use Composer. How to install Composer To get started, download Composer from

The ULTIMATE guide to setup Windows Subsystem for Linux (WSL) with Ubuntu, Apache2, MySQL and PHP

Updated 16th August 2022

Windows is now good for development! Yes! All thanks to Windows Subsystem for Linux (WSL). No more WAMP or Xampp, WSL can do everything you need. In this ultimate guide to setup Windows Subsystem for Linux with Ubuntu, Apache2, MySQL, and PHP we will cover: What is Windows Subsystem for Linux? Setting up Windows Subsystem