Javatpoint Logo
Javatpoint Logo

Laravel Tinker

Laravel Tinker allows you to interact with a database without creating the routes. Laravel tinker is used with a php artisan to create the objects or modify the data. The php artisan is a command-line interface that is available with a Laravel. Tinker is a command tool that works with a php artisan. A tinker plays around the database means that it allows you to create the objects, insert the data, etc.

  • To enter the Tinker environment, run the command given below:

php artisan tinker

Laravel Tinker

The above screen shows that the tinker environment has been created.

Creating data

  • We can create the records in database tables by using the command-line tool. We use the following statement in the command-line tool that inserts the data directly in the database table:
    $post=App\Post::create(['title'=>'Akshay','body'=>'akshay is a software developer']);
Laravel Tinker

Output

When we execute the above statement, the data gets inserted in a posts table. We can view the inserted data in a phpMyAdmin shown in the below screenshot:

Laravel Tinker

We can also use another way to insert the data by creating an object.

  • First, we create the object.
Laravel Tinker

In the above screen, the highlighted line is creating the object, and the name of the object is $post. The $post is the object of the App\Post class.

We can also see whether the $post object has been successfully created or not of a given class by specifying the name of the object in a command-line tool.

Laravel Tinker

The above-highlighted area shows that the $post object has been successfully created as $post shows the name of the class, App\Post.

  • After the creation of an object, we will insert the data with the help of an object.
Laravel Tinker

In the above screen, we have assigned the values to the column's title and body in a posts table by using the statements $post->title and $post->body, respectively. But, still, the data is not inserted in a table. To insert the data in the table, we need to use the statement given below:

$post->save(); // It saves the records in a database table.

When we type the $post in a command-line tool,

Laravel Tinker

The above-highlighted area shows that the record is saved in a posts table

Let's look at the posts table in phpMyAdmin

Laravel Tinker

Finding Record

We can retrieve the records from the database in three ways:

  • The first way is to use the find() method.
Laravel Tinker
  • The second way is to use the constraint, i.e., where clause.
Laravel Tinker

In the above screen, we are retrieving the record, which is having an 'id' equal to 1. In this case, we use the first() method as the first() method is used to retrieve the single record.

Laravel Tinker

In the above screen, we are retrieving the records having an id greater than 1. In this case, more than one record is fetched, so we use the get() method. As get() method is used when an array of records is retrieved.

  • The third way is to use whereId().
Laravel Tinker

Updating data

In this section, we will learn how to update the data in a database.

Let's understand through an example.

  • First, we find out the object which we want to update.
Laravel Tinker

In the above screen, we have retrieved the second record and stored in a $post object.

  • Now we update the values of two columns, title, and body.
Laravel Tinker
  • To save the record in a database, we use the save() method.
Laravel Tinker

In the above screen, the save() method returns true, which means that the record has been successfully updated in a database.

Laravel Tinker

Deleting data

Now, we will see how to delete the data from the database table.

Let's understand through an example.

  • As we know that $post is an object that contains the second record, first, we apply the delete() on the $post object.
Laravel Tinker

The above screen shows that the delete() method returns true value, which means that the record has been deleted.

  • Now, we will look at the database whether the record having an id equal to 2 is actually deleted or not.
Laravel Tinker

As we can observe from the above screenshot that the record of 'id' 2 is still available in the table, but a date is not null in the deleted_at column, which means that this record is soft-deleted.

  • To delete the record permanently,
Laravel Tinker

In the above screen, we use $post->onlyTrashed() means that the $post object contains only a trashed record.

To delete the trashed record permanently, we use the forceDelete() method on a $post object.

Laravel Tinker

The above screen shows that the forceDelete() method returns a true value, which means that the record is successfully removed from the table.

Let's look at the database:

Laravel Tinker

In the above screenshot, we observe that the record of 'id' 2 is deleted from the posts table.

Relationship with tinker

Till now, we find the relationship by using routes. Now, we will see the relationship through the tinker. In a laravel relationship topic, we read a one-to-one relationship with the help of the routes in which we find the post belonging to every user. Now, we find the post of every user in the tinker environment.

Let's understand through an example.

  • First, we look at the data available in both the tables, users and posts table.

users table

Laravel Tinker

posts table

Laravel Tinker
  • In this step, we will find the user.
Laravel Tinker

In the above screenshot, we observe that the $user object contains the second user, i.e., a record of 'id' equal to 2 in the 'users' table.

  • Now, we implement the posts() method available in the User model through the $user object. The statement '$user->posts' calls the posts method of a User class.
Laravel Tinker

The above screen shows that the statement '$user->posts' retrieves the post of the user from the 'posts' table.


Next TopicLaravel Crud





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA