Table of contents
- Let’s dive in and learn some tricks of Laravel
- 🌟One “where” instead of multiple “where” query
- 🌟 Do you know Eloquent’s find() can accept multiple rows
- 🌟 Specify the columns needed
- 🌟 Chunk data for data-heavy tasks
- 🌟 Model Scope
- 🌟 Utilizing A Local Scope
- 🌟 Hide any column in the collection
- 🌟 How to make visible if needed
- 🌟 Replicating Models
- 🌟 Create additional things when creating a model
- 🌟 orWhere with multiple parameters
- 🌟 Maintenance Mode
- 🌟 Soft-deletes : multiple restore
Let’s dive in and learn some tricks of Laravel
Welcome back to the 2nd article of Laravel Tips and Tricks😎. Before we get started, I’d recommend reading the last blog of this series “Laravel Tricks You Should Know Part-1”. Though this article is entirely independent it would be beneficial for you to start from the first blog or else you will miss the important tricks.😉
🌟 Query builder ‘where’ query on a couple of columns?
Change Name and Email as required - the important part is the And between them.
But wait now the question❓ is it possible to do the Or as well??? 🤔
Yeah it is possible to do so let me show you with the same example above
🌟One “where” instead of multiple “where” query
🔥 See we replace the 4 lines of code with just one line of code
🌟 Do you know Eloquent’s find()
can accept multiple rows
$user = User::find(1);
But do you know we can pass the array of ids in find Eloquent.
Note
- If we pass an array to the find() method it will return a collection of Eloquent model rows with those IDs.
- This also applies to findOrFail(). They both (eventually) call findMany() if passed an array of IDs to find.
🌟 Specify the columns needed
$posts = Post::all(['id', 'title', 'description']);
Here we don’t need to call the Post::all().
🌟 Chunk data for data-heavy tasks
If you need to work with thousands of database records, consider using the chunk method. This method retrieves a small chunk of results at a time and feeds each chunk into a Closure for processing. For example, let's retrieve the entire user's table in chunks of 200 records at a time.
It will process 200 records each time. We need it when we are working with mail, data import, and all.
🌟 Model Scope
Scoping is one of the superpowers that eloquent grants to developers when querying a model. Scopes allow developers to add constraints to queries for a given model. For example, you may need to frequently retrieve all users that are considered “popular”. To define a scope, prefix an Eloquent model method with scope.
🌟 Utilizing A Local Scope
🌟 Hide any column in the collection
//app/User.php
/**
* The attributes that should be hidden for arrays.
* @var array
*/
protected $hidden = ['phone_number','pin_code','address'];
🌟 How to make visible if needed
The makeVisible
the method makes attributes visible that are typically "hidden" on each model in the collection.
$users = $users->makeVisible(['address', 'phone_number']);
There is one more method we have makeHidden($attributes)
$users = $users->makeHidden(['address', 'phone_number']);
The makeHidden
method hides attributes that are typically "visible" on each model in the collection:
🌟 Replicating Models
You may create an unsaved copy of an existing model instance using the replicate method. This method is particularly useful when you have model instances that share many of the same attributes:
//The best way to make a copy of database entry
$post = Post::find(1);
$newPost = $post->replicate();
$newPost->save();
🌟 Create additional things when creating a model
php artisan make:model Company -mcr
-m will create a migration file
-c will create a controller
-r will indicate that controller should be resourceful
🌟 orWhere with multiple parameters
// Instead of:
$q->where('a', 1);
$q->orWhere('b', 2);
$q->orWhere('c', 3);
// Do this
$q->where('a', 1);
$q->orWhere(['b' => 2, 'c' => 3]);
🌟 Maintenance Mode
If you want to enable maintenance mode on your page, execute the down Artisan command:
php artisan down
When you’ve done the maintenance work, just run
php artisan up
🌟 Soft-deletes : multiple restore
//When using soft-deletes, you can restore multiple rows in one sentence.
Post::withTrashed()->where('author_id', 1)->restore();
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/