How to Insert a New Record Using PHP Artisan Tinker in Laravel

Posted on

This article is an article which is focusing on the writing for a specific subject specified in the title of this article. It is about how to insert a new record using PHP Artisan Tinker in Laravel. To simplify or to insert a new record in a simple way without having to connect to MySQL Database Server as long as the connection itself has been made from inside the Laravel web-based application project. Just execute the following command to start inserting a new record using PHP Artisan Tinker :

php artisan tinker

Remember correctly that the above command need a certain prerequisite in order to be executed. The following are :

1. The installation of PHP program.

2. The installation of a Laravel web-based application into a certain specific folder.

3. The availability of artisan tool which is already included normally in a Laravel web-based application if it is generated or created with the help of composer utility with the command of ‘composer create-project laravel/laravel project_name’.

4. The availability of tinker tool which is already included normally in a Laravel web-based application if it is generated or created with the help of composer utility with the command of ‘composer create-project laravel/laravel project’.

5. The connectivity to the database which is intended to be the main data source operation.

6. The model which is associated with one of the table. To be able to create a model, just refer to an article titled ‘How to Create a Model in Laravel using PHP Artisan Command‘ or in another article titled How to Create Model with Migration Script in Laravel .

7. After all of the above steps are carried out, the following is the execution of ‘php artisan tinker’ :

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
>>> $category = new App\Category
=> App\Category {#734}
>>> $category->name = "Technical"
=> "Technical"
>>> $category->save()
=> true
>>> quit
Exit: Goodbye.
user@hostname:/var/www/html/laravel$

It is actually inserting a new record using a model named Category located in the App folder inside the Laravel web-based application project’s folder. It is creating a new instance of a model named Category and it is stored to a variable named $category. The instance’s attribute named ‘name’ is defined with a value of ‘Technical’. After the initialization of the attribute, it is saved. The process is terminated as soon as the ‘quit’ command typed.

One thought on “How to Insert a New Record Using PHP Artisan Tinker in Laravel

Leave a Reply