DEV Community

Mahmoud Ramadan
Mahmoud Ramadan

Posted on • Edited on

πŸ’‘ Two Lesser-Known Laravel Tricks: numerify & invisible

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
    ];
}
Enter fullscreen mode Exit fullscreen mode

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();
});
Enter fullscreen mode Exit fullscreen mode

πŸš€ Find more tips and share your knowledge here:
https://github.com/digging-code-blog/community-tips

Top comments (6)

Collapse
 
xwero profile image
david duymelinck • Edited

From the Laravel code

 * @method $this invisible() Specify that the column should be invisible to "SELECT *" (MySQL)
Enter fullscreen mode Exit fullscreen mode

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 $hidden property of the Eloquent model.

Collapse
 
mmramadan496 profile image
Mahmoud Ramadan

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 README file regarding that point.

Collapse
 
xwero profile image
david duymelinck

Thank you, but it is not necessary.

Thread Thread
 
mmramadan496 profile image
Mahmoud Ramadan

Glad to help! Just wanted to give proper credit.

Thread Thread
 
xwero profile image
david duymelinck

Give credit to the Laravel code, I'm just the one that read it 😊

Thread Thread
 
mmramadan496 profile image
Mahmoud Ramadan

Yup! I’ve already updated the code in this post and across the entire site. πŸ™