Ø Tinker is a REPL, or read–eval–print loop. If
you’ve ever used IRB in Ruby, you’ll be familiar with how a REPL works.
Ø
REPLs give you
a prompt, similar to the command-line prompt, that mimics a “waiting” state of
your application. You type your commands into the REPL, hit Return, and then expect
what you typed to be evaluated and the response printed out.
Ø
Example 7-13 provides a quick
sample to give you a sense of how it works and how it might be useful. We start
the REPL with php artisan tinker and are then presented with a blank prompt
(>>>); every response to our commands is printed on a line prefaced
with =>.
php artisan tinker
>>> $user = new App\User;
=> App\User: {}
>>> $user->email = 'matt@mattstauffer.co';
>>> $user->password =
bcrypt('superSecret');
=>
"$2y$10$TWPGBC7e8d1bvJ1q5kv.VDUGfYDnE9gANl4mleuB3htIY2dxcQfQ5"
>>> $user->save();
=> true
Ø
As you can see,
we created a new user, set some data, and saved it to the database. And this is
real. If this were a production application, we would’ve just created a brand new
user in our system.
Ø
This makes
Tinker a great tool for simple database interactions, for trying out new ideas,
and for running snippets of code when it’d be a pain to find a place to put
them in the application source files.
No comments:
Post a Comment