Skip to content

Instantly share code, notes, and snippets.

@mishajib
Last active July 16, 2019 19:21
Show Gist options
  • Save mishajib/4ac2958e2e5afefc9f821b912f291308 to your computer and use it in GitHub Desktop.
Save mishajib/4ac2958e2e5afefc9f821b912f291308 to your computer and use it in GitHub Desktop.
Laravel Tinker CRUD Operation Command
For Using Laravel Tinker First You have to run the below command:
=======================================================================
"php artisan tinker"
CREATE OPERATION
====================
DB::table("companies")->insert(["name"=>"mi","email"=>"abc@gmail.com","location"=>"demo_location","created_at"=>new DateTime])
Single row insertion
=====================
DB::table("table_name")->insert(["key"=>"value","key"=>"value"])
Multi-row insertion
========================
DB::table("table_name")->insert([["key"=>"value","key"=>"value"], ["key"=>"value","key"=>"value"], ["key"=>"value","key"=>"value"] ....])
READ/SELECT/FETCH OPERATION
============================
DB::table("table_name")->get()
DB::table("companies")->where("email","abc@gmail.com")->get() //Get the particular information from database by checking the "Where" condition.
DB::table("companies")->where("email","abc@gmail.com")->first() //Get the particular information from database by checking the "Where" condition.
For multiple condition
=======================
DB::table("table_name")->where(["key"=>"value"....])->first()
To get particular column values
===============================
DB::table("companies")->pluck('name')
To get multiple column values
==============================
DB::table('companies')->select('name','email')->get()
Serialize by Ascending or Descending order
===========================================
DB::table('companies')->orderBy("id",'desc')->get()
Aggregate Functions
=====================
DB::table("companies")->count() - count the row from the table
DB::table("companies")->max('id')
DB::table("companies")->min('id')
DB::table("companies")->avg('id')
UPDATE OPERATIONS
====================
DB::table("companies")->where("id",2)->update(["email"=>"update@gmail.com"])
DB::table("companies")->where("id",3)->update(["email"=>"update@gmail.com","location"=>"uttara"])
DELETE OPERATIONS
======================
DB::table("companies")->where("id",4)->delete() //Delete id 4 row
DB::table("companies")->delete() //Delete all row from table
Difference Between get() & first() method
==========================================
get() => returns a collection object containing "Database" models for all records that match the WHERE condition.
first() => returns a "Database" model object of just one (the first) match for the WHERE clause.
Tinker Factory Command
=========================
Laravel provides a special model factory that can be used to seed database tables.
It can seed dummy records with a single tinker command.
By using this command we can create/insert multiple dummpy data at a single time.
Before using this command you have to run => "php artisan tinker"
"factory(App\model_name::class, 8)->create();"
EX - factory(App\User::class, 8)->create();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment