DEV Community

Robin  Dev
Robin Dev

Posted on • Originally published at larachamp.com on

10 Quick Tips About Laravel by Robin Kashyap

10 Quick Tips About Laravel by Robin Kashyap

As you know Laravel is going to be a huge framework and it has many ways to do same things. After many days I thought that I should share laravel tips and tricks which I have learned from the environment I have on social media. Well, most of the time I do use twitter and on twitter lot of content you will see around laravel tips. Maximum time I saw a person Gurmandeep Singh never failed to share about the tips about Laravel.

Today we gonna learn some tips about Laravel may some short ways to do bigger tasks or some smart ways to do the usuall things.

Laravel Tip – Authentication tip

You might know that we have lot of directives to work with blade files in laravel. So, In the same way when it comes to authentication, we do have some directives that you can use wisely in your applications.

@auth
//if user is authenticated
@endauth

@guest
//if user is not authenticated can apply code here
@endguest
Enter fullscreen mode Exit fullscreen mode

If you are authenticating use then if you have some functionality where you do provide something that an unauthenticated user can use. The whole code you can pass under guest directive and vise versa.

Laravel Tip – RandomOrder

If you are fetching results from the model and want to get the result in random order. You can use the ‘in random order’ method to sort the data in random order.

use App\MOdel\User

return User::inRandomOrder()->get();
Enter fullscreen mode Exit fullscreen mode

Laravel Tip – Eloquent

If you do work with relationships in Eloquent Models. Then you can consider using this tip which is loadMissing. loadmissing method to load a relationship only when it has not already been loaded.

$users->loadMissing(['comments','posts']);
Enter fullscreen mode Exit fullscreen mode

Laravel Tip – Use Statement

Before implementing anything out of the present file. For an example : we are working in controller and we want to fetch some data from users and then from books then what we do is to include them first in the script. So, that case we write code as below:

//before
use App\Models\User;
use App\Models\Book;
use App\Models\Comment;
Enter fullscreen mode Exit fullscreen mode

But we can make it much shorter to use.

//after
use App\Models\{Comment,Book,User};
Enter fullscreen mode Exit fullscreen mode

Laravel Tip – Migration

If you do not create database manually and you do use migrations then this tip is for you. When you are creating a model then you can use –migration or -m to create migrations along with that.

php artisan make:model Product -m 
Enter fullscreen mode Exit fullscreen mode

findOr In Laravel tip

Similar to findOrFail method , you can use the ‘findOr’ method which returns a single model instance or if no results are found then it will execute the given closure. It’s almost same as FindOrFail.

<?php

namespace App\Http\Controllers;
use Illuminate\Https\Request;
use App\Models\Book;

public function show($id) {
return Book::findOr($id,fn () => abort(403) )
}
Enter fullscreen mode Exit fullscreen mode

Validation Tip

You can use in built rule of laravel which says that you can use min-digits and ``max-digits’ in validation to validate the numbers. For example you are validating the mobile number and in that case you can validate the digits as above explained.

`
Validator::validate([
'number'=> 1000,
],
'number' => [
'min-digits' => 3, 'max-digits' => 5,
],
);
`

In this way you can validate the numbers

“Updated_at” Tip

This tip is bit short and smart from others. If you want to update the model updated_at column then you can simply use the “touch()” method.

`
use App\Models\User;
public function update(){
$user = User::find(1);
$user-touch();
}
`

Array Tip

You can easily prepend the data using “Arr::prependArrKeysWIth” method.

`
use Illuminate\Support\Arr;

$array = [
'name' => 'Shruti',
'class' => 'fourth',
];

$change = Arr::prependKeysWith($array,'fake.');

//then the result will be

[
'fake.name' => 'shruti',
'fake.class' => 'fourth',
]

`

Routing Tip

As for example, If you are writing routes for the same controller then you can write all the routes under controller group rather than passing controller for single single route. See the code below:

`
//Before - passing controller for each route
Route::get('/dashboard',[ServiceProviderController::class,'spdashboard'])->name('serviceprovider.dashboard');
Route::get('/verifyUpdateMobile',[ServiceProviderController::class,'verifyUpdateMobile')->name('verifyUpdateMobile');

//after grouping the routes for same controller
Route::controller(ServiceProviderController::class)->group(function () {
Route::get('/dashboard', 'spdashboard')->name('serviceprovider.dashboard');
Route::post('verifyUpdateMobile', 'verifyUpdateMobile');
});
`

You also can read about Laravel Interview Questionshere.

Conclusion

So, Basically these are just quick tips about the laravel but next I’m preparing for a huge list of laravel tips and will share with you. You can subscribe to youtube for updates or you also can visit website for laravel related content. If you found something helpful and want to connect then come on gurpreetkait’s Twitter

The post 10 Quick Tips About Laravel by Robin Kashyap appeared first on Larachamp.

Top comments (0)