padam thapaIn beginning of my Laravel learning, I did write more lines of code to perform my task than I write now. I started learning to adopt more eloquent ways of coding for Laravel encourages us to write. One simple task I performed by writing this many number of lines:
$place = PlaceModel::where('id', "132")->first();
if(isset($place))
{
$place->update(['name' => 'Place Name', 'address' => 'Place Address',]);
}
else
{
$place = PlaceModel::create([
'name' => 'Place Name',
'address' => 'Place Address',
]);
}
This many lines of code can be optimised to single statement of line in Laravel Eloquent way. This is ome of the ways to optimize eloquent query on Laravel:
We must have stumbled on Laravel’s passport sometime for creating authentication api’s for our mobile apps providing full OAuth2 server implementation.
This post uses Laravel 7.x. Our passport installation is
composer require laravel/passport “~9.0"
We will perform the migration where Passport migrations will create the tables our application needs to store clients and access tokens. It will also create Users table for us.
php artisan migrate
We also updated the driver to use passport in config/auth.php
'api' => [
'driver' => 'passport',
'provider' => 'users',
'hash' => false,
],
Now in our registration api example when we try to generate access token
$user->createToken('MyApp Password Grant Client’)->accessToken;
Auth driver [passport] for guard [api] is not defined.
Personal access client not found.