How to Read Data from Database in Tinker.

To read data from the database, we use the below commands:-

1. DB::table(“Table_name“)->get() :- It returns all the data present in the given table of database. See below:-

2. DB::table(“table_name”)->where(“key1″,”key1_value”)->first() :- It returns all data of the row in which key value found. See below:-

3. DB::table(“table_name”)->pluck(“column_name”); :- It returns all the data stored in the given column from database.

Pluck :- pluck is a method in laravel which is used when we have to fetch a particular column from database.

4. DB::table(“Table_name”)->select(“column1”, “column2”, “column3”)->get() :- It is used to return the given data of the row. Means, if we call name and E-mail column then it returns the both with row-wise. See below how it works:-

5. DB::table(“Table_name”)->count() :- It returns the no. of rows. See below:-

There are 5 rows in my database so it returns 5.

6. DB::table(“Table_name”)->max(“ Column_name “) :- It returns the maximum id no. of your data from database. See below:-

See the Database Below:-

7. DB::table(“Table_name”)->min(“ Column_name “) :- It returns the minimum id no. of your data from database. See below:-

See the Database Below:-

8. DB::table(“Table_name”)->min(“Column_name”) :- It returns the average value of the given column.

Suppose I want the average value of the id column. See below:-