11 things we can do with Laravel Tinker

Laravel has a tool called Tinker. Tinker is Laravel’s REPL, or Read Evaluate Print Loop. Its allow you to perform complex PHP operations within the full context of your app from the command line.

When we setup Laravel, we got this tool automatically. We don’t need to install it separately

With tinker we can do many things, for example :

1. Send email

>>> Mail::send('emails.mail', [], function ($message) { $message->to('[email protected]')->subject('Tes Tinker Send Email!'); });

2. Create User

>>> \App\User::create([
	"name" => 'John Doe',
	"email" => '[email protected]',
	"api_token" => Str::random(60),
	"password" => bcrypt("s3cr3tp455w0rd"),
	"user_type" => '1'
]);

3. Count all users

>>> App\User::count();

4. Find a specific user and see their attributes

>>> App\User::where('username', 'john')->first();

5. Create dummy users

>>> factory(App\User::class, 10)->create();

6. Delete a user

>>> $user = App\User::find(1);
>>> $user->delete();

7. Get today date with carbon

>>> Carbon\Carbon::now()

8. Check if a date is in the past

>>> $now = \Carbon\Carbon::now();
>>> $now->diffInSeconds();
>>> $now->diffInSeconds() > 0

9. Substract and format a date

>>> Carbon\Carbon::now()->subDays(1)->format('d_m_Y')

10. Get a date after particular time

>>> Carbon\Carbon::now()->addWeeks(4)->format('d-m-Y')

11. Encrypt or decrypt a string

>>> \Crypt::encryptString('Hello world.');
=> "eyJpdiI6IklmNUhrbzI2VXVWbHJuQS9BMzZLRmc9PSIsInZhbHVlIjoiR2Mzc2VPUTkxUmJRb0swclprQlFXQT09IiwibWFjIjoiNGRhMjczMjJkYjc2MDRlZmZjMmE4YzlkYmZjNDJlZmFkNmUxODI2YzU2MWJhYjRjYjNmMGE3ZTg2YzU5YjFiMiJ9"
>>> \Crypt::decryptString('eyJpdiI6IklmNUhrbzI2VXVWbHJuQS9BMzZLRmc9PSIsInZhbHVlIjoiR2Mzc2VPUTkxUmJRb0swclprQlFXQT09IiwibWFjIjoiNGRhMjczMjJkYjc2MDRlZmZjMmE4YzlkYmZjNDJlZmFkNmUxODI2YzU2MWJhYjRjYjNmMGE3ZTg2YzU5YjFiMiJ9');
=> "Hello world."
>>>

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>