How to Use php artisan tinker to Insert Data

Posted on

This article simply written to show how to use php artisan tinker to insert data in a database specified or used by a Laravel web-based application created. So, this is working for any Laravel web-based application project which is already connected to the database and there are already models created which are associated to each table exist in the database used.

user@hostname:/var/www/html/laravel$ php artisan tinker
Psy Shell v0.8.13 (PHP 7.0.24-1+ubuntu16.04.1+deb.sury.org+1 — cli) by Justin Hileman
>>> $service = new App\Service();
=> App\Service {#741}
>>> $service->name = "PC Installation";
=> "PC Installation"
>>> $service->save();
=> true
>>> quit
Exit: Goodbye.
user@hostname:/var/www/html/laravel$

The above output are printed by executing the command one by one with the sequential way. The first one :

>>> $service = new App\Service();
=> App\Service {#741}

The above command is executed to create a new instance for further operation. The output generated by tinker is the instance identifier.

The second one :

>>> $service->name = "PC Installation";
=> PC Installation

The above command execution will set the attribute owned by the instance named ‘service’ with a value of ‘PC Installation’.  The output produced by tinker is actually printing the value of the attribute which is ‘PC Installation’.

The third one  :

>>> $service->save();
=> true

The above command execution will finally save the instance into its corresponding table. And the following line which is actually an output produced by tinker stated a true condition which indicating the operation of saving the instance into a record located in its corresponding table has already succeed.

>>> quit

The last command ‘quit’ is just a command to quit from the tinker utility.

Check it in the database associated, more specific in the table which is represented with the associated model used and modified its instance in the above output command using tinker by setting one of its attribute which in the above context it is setting the attribute ‘name’ with the value of ‘PC Installation’.

So, to prove that there is a record added into the table named ‘services’ with the column named ‘name’ has its value inserted with the value of ‘PC Installation’, just logged in to MySQL Command Console as shown below :

user@hostname:/var/www/html/laravel$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.7.19-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use support_ticket;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from services where name like '%PC%';
+----+-----------------+---------------------+--------------------+
| id | name            | created_at          | updated_at         |
+----+-----------------+---------------------+--------------------+
| 1  | PC Installation | 2017-11-03 09:27:11 | 2017-11-03 09:27:11|
+----+-----------------+---------------------+--------------------+
1 row in set (0,00 sec)

mysql>

As it can be seen above, there is a new entry of record with the column named ‘name’ inserted with the value of ‘PC Installation’ which is actually the same value executed using php artisan tinker.

Leave a Reply