logo

Laravel tips and tricks

Laravel tips and tricks

Laravel is the most popular PHP and web development framework. Being powerful, secured, easy, and developer-friendly many developers learning it. Here are some tips for a beginner Laravel developer.



1. Separate routes

In a big project, the route file could be very large and messy. By separating routes for the feature it could be nice. Like if you want to separate admin routes you can simply create a file in the routes directory as admin.php. and you have to use the route faced in admin.php


use Illuminate\Support\Facades\Route;

in web.php inside routes folder add the admin.php like this

require __DIR__.'/admin.php';



2. Rate limiting

Rate limiting is very important to prevent DDoS attacks. You can limit the requests for specific routes by using route middleware easily like

Route::group(['middleware' => 'throttle:60,1'], function () {

 Route::get('your_route', [YourController::class, 'your_method']);

});


or like this

Route::middleware('throttle:60,1')->group(function () {

 Route::get('your_route', [YourController::class, 'your_method']);

});

Here in 1 minute maximum of 60 requests can be sent.


You can specify the limit for the guest and user like this

Route::middleware('throttle:30|60,1')->group(function () {

 Route::get('your_route', [YourController::class, 'your_method']);

});

Here a maximum of 30 requests for guests and 60 requests for authenticated users can be sent.



3. Display route list cleaner way

To see all your route list you can run the following command

php artisan route:list


This will return all data which takes too much space is not easy to read. To display only Method, URI and Action you can run this command

php artisan route:list --compact


To see specific columns you can specify the column names like this

php artisan route:list --columns=Method,URI,Name



4. Check if the view file exists

You can check if the view file exists before loading it like this:


if (view()->exists('folder.file')) {

 // Load the view

}

You can load priority basis view file. the first file will load. like:

return view()->first(['folder.file', 'file'], $data);



5. Render view file without a controller

You can render the view file without a controller from the route file like this

Route::view('about', 'texts.about');



6. Maintenance mode

While upgrading your application to disable your application and go for maintenance mode easily you can run the following command

php artisan down


It will redirect all your requests to maintenance mode custom view. If the application is in maintenance mode, a MaintenanceModeException will be thrown with a status code of 503. You may also provide a retry option to the down command, which will be set as the Retry-After HTTP header's value:

php artisan down --retry=60



7. Simple Pagination

If you want to show only the next and previous button in your pagination links without retrieving the total number of pages, posts, and page numbers you can do it with simplePaginatemethod like this:


$users = DB::table('users')->simplePaginate(15);



8. Redirect to Specific Controller Method

If you want to redirect any controller method rather than route name you can do it like this:


use App\Http\Controllers\UserController;

return redirect()->action([UserController::class, 'index']);



9. If not exists redirect to 404

If you wish to throw an exception if a model is not found you can use findOrFail or firstOrFail. If model row is exists it will return the row otherwise Illuminate\Database\Eloquent\ModelNotFoundException will be thrown


$flight = Flight::findOrFail(1);

$flight = Flight::where('legs', '>', 3)->firstOrFail();



These tips and tricks are compatible with Laravel 8.

At 18th Apr 2023
Share Content:
Read: 24

Recent Comments