2026 Executive BenchmarkMid-Market Enterprise Modernization: How leaders across unify Commerce, Odoo ERP & AI in 90 days.
LARAVEL FRAMEWORK

Laravel Tips & Trickes 2022 | Easy To Use | Mind Blowing

5 Tips & Tricks of Laravel Laravel is an open-source PHP framework designed to make developing web apps easier and faster through built-in features. These features are part of what...

Akshar Group Technologies
May 2, 2022
2 min read
Verified Source

5 Tips & Tricks of Laravel

Laravel is an open-source PHP framework designed to make developing web apps easier and faster through built-in features. These features are part of what makes Laravel so widely used by web developers:

But in this blog, we are going to learn the top most tips and tricks of Laravel so that we can use this in our development and make our development easier. So lets not waste any more time and lets gets started.

Tip 1 : $loop variable in foreach

Inside of foreach loop, check if current entry is first/last by just using $loop variable.

php
@foreach ($users as $user)
@if ($loop->first)
This is the first iteration.
@endif
@if ($loop->last)
This is the last iteration.
@endif
<p>This is user {{ $user->id }}</p>
@endforeach

Tip 2 : Route group within a group

in Laravel Routes, we can create a group within a group, assigning a certain middleware only to some URLs in the parent' group.
php
Route::group(['prefix' => 'user', 'as' => 'user.'], function() {
Route::get('login', 'UserController@login');
Route::get('register', 'UserController@register');
Route::group(['middleware' => 'auth'], function() {
Route::get('edit', 'UserController@edit');
});
});

Tip 3 : No timestamp columns

If our database table doesn't contain timestamp fields created_at and updated_at, so we can

specify that Eloquent model wouldn't use them, with $timestamps = false property.
php
class User extends Model
{
public $timestamps = false;
}

Tip 4 : Don't create Controllers for certain view

If we want route to just show a certain view, than don't create a Controller method, just use

direct Route::view() function on web.php file.
css
// Instead of using controller this
Route::get('create', 'UserController@create');
// And this
class TextsController extends Controller
{
public function create()
{
return view('user.create');
}
}
// Do this on web.php file
Route::view('user', 'user.create');

Tip 5 : Blade @auth (user authenticated or not)

Instead of if-statement to check logged in user is authenticated or not, use @auth directive.
code
//Using if-statement:
@if(auth()->user())
// The user is authenticated.
@endif
//Using @auth directive:
@auth
// The user is authenticated.
@endauth
Share this playbook
LET'S BUILD WHAT'S NEXT

What's Holding Your Business Back?

Tell us where you want to go, what's getting in the way, and we'll help you figure out the technology to get there.

No sales pitch. Confidential strategic advisory.