Laravel Tricks You Should Know 😇
Let’s dive in and learn some tricks of Laravel
Table of contents
- 🌟 API Resources: With or Without “data”?
- 🌟 Avoid N+1 queries in API resources
- 🌟 Get a Bearer Token from the Authorization header
- 🌟 Exact Laravel version
- 🌟 How to create a custom command
- 🌟 Hide your custom command
- 🌟 Eloquent where date methods
- 🌟 Increment & Decrement
- 🌟 No timestamp columns
- 🌟 Default Timestamp
- 🌟 Column name change
- 🌟 More convenient DD
- 🌟 Never run “composer update” in production
- 🌟 Change Default Timestamp Fields
- 🌟 Order by created_at
Let's learn some important tricks of laravel.This is the 1st article of Laravel Tips and tricks series.
🌟 API Resources: With or Without “data”?
If you use Eloquent API Resources to return data, they will be automatically wrapped in ‘data’ (data.data). You can get rid of that extra inner data wrap by the below process.
🌟 You can also customize what the inner data element should be, say you want to return data. polls instead of data.data
public static $wrap = ‘polls’;
Example
🌟 Avoid N+1 queries in API resources
The whenLoaded method may be used to conditionally load a relationship. In order to avoid unnecessarily loading relationships, this method accepts the name of the relationship instead of the relationship itself. Without whenLoaded() there is always a query for the posts.
Example
🌟 Get a Bearer Token from the Authorization header
🌟 Exact Laravel version
First cd into your web app and run the below command to get the laravel version.
php artisan --version
🌟 How to create a custom command
php artisan make:command SendMailToAllUser
The above command will create a custom command in your “/app/Console/Commands”
🌟 Hide your custom command
The code will hide the custom command from the artisan list
🌟 Eloquent where date methods
“ whereDate / whereMonth / whereDay / whereYear / whereTime ”
- whereDate: The
whereDate
method may be used to compare a column's value against a date:
DB::table('polls')->whereDate('created_at','2022-06-01')->get();
- whereMonth: The
whereMonth
method may be used to compare a column's value against a specific month:
DB::table('polls')->whereMonth('created_at', '06')->get();
- whereDay : The
whereDay
the method may be used to compare a column's value against a specific day of the month:
DB::table('polls')->whereDay('created_at', '01')->get();
- whereYear: The
whereYear
the method may be used to compare a column's value against a specific year:
DB::table('polls')->whereYear('created_at', '2022')->get();
- whereTime: The
whereTime
the method may be used to compare a column's value against a specific time:
DB::table('polls')->whereTime('created_at', '=', '07:05:45')->get();
🌟 Increment & Decrement
The query builder also provides convenient methods for incrementing or decrementing the value of a given column. Both of these methods accept at least one argument: the column to modify. A second argument may be provided to specify the amount by which the column should be incremented or decremented.
#Increment the count
DB::table('polls')->increment('votes');
DB::table('polls')->increment('votes', 5);
#Decrement the count
DB::table('polls')->decrement('votes');
DB::table('polls')->decrement('votes', 5);
🌟 No timestamp columns
If your DB table doesn’t contain timestamp fields created_at and updated_at, you can specify that the Eloquent model wouldn’t use them, with $timestamps=false property.
🌟 Default Timestamp
While creating migrations, you can use ->timestamp() column type with option ->useCurrent(), it will set CURRENT_TIMESTAMP as the default value.
🌟 Column name change
In Eloquent Query Builder, you can specify “as” to return any column with a different name, just like in a plain SQL query.
🌟 More convenient DD
🌟 Never run “composer update” in production
Never run the composer update on production, it’s slow and will “break” the repository. Always run the composer update locally on your computer, and commit a new composer.lock to the repository, and run composer install on the server.
🌟 Change Default Timestamp Fields
What if you’re working with a non-Laravel database and your timestamp columns are named differently? Maybe, you have create_date and update_date. Luckily, you can specify them in the model.
🌟 Order by created_at
By default, the latest() will order by created_at. There is an opposite method oldest() which would order by created_at ascending.
User::oldest()->get();
Stay tuned!!! will be back with some more cool laravel tricks in the next article. Don’t forget to follow me 😇
Credit goes to --> https://laravel.com/
Thanks a lot for reading till the end 🙏 You can contact me in case if you need any assistance:
Email: atul19251@gmail.com
Web: https://iamatul.netlify.app/
GitHub: https://github.com/iamrajput
LinkedIn:https://www.linkedin.com/in/atul-kumar-singh-673357102/