Laravel is full of hidden gems that can make your development smoother. Today, I want to highlight two handy features you might not know about:
1οΈβ£ numerify in Eloquent Factories
The numerify method from Faker lets you replace # characters with random digits.
public function definition(): array
{
return [
'phone' => $this->faker->numerify('+##'), // Example output: +20
];
}
2οΈβ£ invisible in Migrations
The invisible column modifier hides a column from default SELECT queries while keeping it in the database.
Note: This feature is supported only by the MySQL database engine.
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
Schema::create('users', function (Blueprint $table) {
$table->timestamp('email_verified_at')
->nullable()
->invisible();
});
π Find more tips and share your knowledge here:
https://github.com/digging-code-blog/community-tips
Top comments (6)
From the Laravel code
This is a MySQL only feature. So running the migration on Postgres will not have the same effect.
I would be very careful using this database feature. A database agnostic way is to use the
$hiddenproperty of the Eloquent model.You're rightβI missed that point. Iβll make sure to highlight it. If youβd like, share your GitHub handle, and Iβll include a mention for you in the
READMEfile regarding that point.Thank you, but it is not necessary.
Glad to help! Just wanted to give proper credit.
Give credit to the Laravel code, I'm just the one that read it π
Yup! Iβve already updated the code in this post and across the entire site. π