Quick Tips - Laravel
Tags: laravel, php, programming, beginners
Published: 2022-01-07 09:24:58

Let's get started quickly and find some tips so I can share their fun with you.

You may assign multiple middleware to the route by passing an array of middleware names to the middleware method
```php
Route::get('/profile', function () {
    //
})->middleware('auth');

Route::get('/', function () {
    //
})->middleware(['first', 'second']);
```
Version Laravel 8 uses more requests help you with your project
https://laravel.com/docs/8.x/requests
```php
$uri = $request->path();

if ($request->is('admin/*')) {
    //
}

if ($request->routeIs('admin.*')) {
    //
}

$url = $request->url();
$urlWithQueryString = $request->fullUrl();

$request->fullUrlWithQuery(['type' => 'phone']);

$method = $request->method();
if ($request->isMethod('post')) {
    //
}
```
When assigning middleware, you may also pass the fully qualified class name
```php
use App\Http\Middleware\EnsureTokenIsValid;

Route::get('/profile', function () {
    //
})->middleware(EnsureTokenIsValid::class);
```
When assigning middleware to a group of routes, you may occasionally need to prevent the middleware from being applied to an individual route within the group. You may accomplish this using the ``withoutMiddleware`` method
```php
use App\Http\Middleware\EnsureTokenIsValid;

Route::middleware([EnsureTokenIsValid::class])->group(function () {
    Route::get('/', function () {
        //
    });

    Route::get('/profile', function () {
        //
    })->withoutMiddleware([EnsureTokenIsValid::class]);
});
```
The findOrFail method also accepts a list of ids
```php
// Retrives the user...
$user = User::findOrFail(1);

// Throws a 404 because the user doesn't exist...
User::findOrFail(99);

// Retrives all 3 users...
$users = User::findOrFail([1, 2, 3]);

// Throws because it is unable to find *all* of the users
User::findOrFail([1, 2, 3, 99]);
```
You can use value() method to get single column's value from the first result of a query
```php
// Instead of
Integration::where('name', 'foo')->first()->active;

// You can use
Integration::where('name', 'foo')->value('active');

// or this to throw an exception if no records found
Integration::where('name', 'foo')->valueOrFail('active');
```
Laravel 8.69 released with "Str::mask()" method which masks a portion of string with a repeated character
```php
$userEmail = User::where('email', $request->email)->value('email'); // username@domain.com
        
$maskedEmail = Str::mask($userEmail, '*', 4); // user***************
        
// If needed, you provide a negative number as the third argument to the mask method
// which will instruct the method to begin masking at the given distance from the end of the string
        
$maskedEmail = Str::mask($userEmail, '*', -16, 6); // use******domain.com
```
You can send e-mails to a custom log file
You can set your environment variables like this
```php
MAIL_MAILER=log
MAIL_LOG_CHANNEL=mail
```
And also configure your log channel
```php
'mail' => [
    'driver' => 'single',
    'path' => storage_path('logs/mails.log'),
    'level' => env('LOG_LEVEL', 'debug'),
],
```
Here we want to copy a column into the database
```php
$post = Post::find(1);
$newPost = $post->replicate();
$newPost->created_at = Carbon::now();
$newPost->save();
```

We want to know the method coming from a specific function or route use `` isMethod `` or `` method() `` to search for it
```php
$request->isMethod('post');
in_array($request->method(),['POST','PUT','PATCH','DELETE']))
```

Auto-Redirect to Login in Breeze or Jetstream
```php
Route::redirect('/', 'login');
```

I hope you enjoyed the code
Source :- https://www.youtube.com/watch?v=bnXauabSFYI
Source :- https://www.youtube.com/watch?v=YzBGmdnGtbY&t=83s
Source :- https://github.com/LaravelDaily/laravel-tips