Last active
February 12, 2024 05:52
-
-
Save jack2jm/f6b24fd3c5eecf7b2004d3cd69896741 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Improve Query Result - Make it faster. | |
| ******** Results ******** | |
| //All Rows | |
| $users = DB::table('users')->get(); | |
| //Single record | |
| $user = DB::table('users')->where('name', 'John')->first(); | |
| echo $user->name; | |
| //perticular column | |
| $email = DB::table('users')->where('name', 'John')->value('email'); | |
| //Retrieving A List Of Column Values | |
| $titles = DB::table('roles')->pluck('title'); | |
| ******** Aggregates ******** | |
| DB::table('users')->orderBy('id')->chunk(100, function ($users) { | |
| foreach ($users as $user) { | |
| // | |
| } | |
| }); | |
| ****** Aggregates ******** | |
| $users = DB::table('users')->count(); | |
| $price = DB::table('orders')->max('price'); | |
| $price = DB::table('orders') | |
| ->where('finalized', 1) | |
| ->avg('price'); | |
| //if record exist or not | |
| return DB::table('orders')->where('finalized', 1)->exists(); | |
| return DB::table('orders')->where('finalized', 1)->doesntExist(); | |
| ****** Select ******** | |
| $users = DB::table('users')->select('name', 'email as user_email')->get(); | |
| //distinct | |
| $users = DB::table('users')->distinct()->get(); | |
| ****** Row Expression ******* | |
| $users = DB::table('users') | |
| ->select(DB::raw('count(*) as user_count, status')) | |
| ->where('status', '<>', 1) | |
| ->groupBy('status') | |
| ->get(); | |
| ******** Join ******** | |
| Inner Join | |
| $users = DB::table('users') | |
| ->join('contacts', 'users.id', '=', 'contacts.user_id') | |
| ->join('orders', 'users.id', '=', 'orders.user_id') | |
| ->select('users.*', 'contacts.phone', 'orders.price') | |
| ->get(); | |
| Left Join | |
| $users = DB::table('users') | |
| ->leftJoin('posts', 'users.id', '=', 'posts.user_id') | |
| ->get(); | |
| Custom Condtion Join | |
| DB::table('users') | |
| ->join('contacts', function ($join) { | |
| $join->on('users.id', '=', 'contacts.user_id') | |
| ->where('contacts.user_id', '>', 5); | |
| }) | |
| ->get(); | |
| ****** Where ******** | |
| $users = DB::table('users')->where('votes', '=', 100)->get(); | |
| or | |
| $users = DB::table('users')->where('votes', 100)->get(); | |
| $users = DB::table('users') | |
| ->where('votes', '>=', 100) | |
| ->get(); | |
| $users = DB::table('users') | |
| ->where('votes', '<>', 100) | |
| ->get(); | |
| $users = DB::table('users') | |
| ->where('name', 'like', 'T%') | |
| ->get(); | |
| //pass multiple condtion | |
| $users = DB::table('users')->where([ | |
| ['status', '=', '1'], | |
| ['subscribed', '<>', '1'], | |
| ])->get(); | |
| //OrWhere | |
| $users = DB::table('users') | |
| ->where('votes', '>', 100) | |
| ->orWhere('name', 'John') | |
| ->get(); | |
| // where between/not between | |
| $users = DB::table('users') | |
| ->whereBetween('votes', [1, 100])->get(); //->whereNotBetween('votes', [1, 100] | |
| // where IN/not in | |
| $users = DB::table('users') | |
| ->whereIn('id', [1, 2, 3]) //->whereNotIn('id', [1, 2, 3]) | |
| ->get(); | |
| // whereNull / whereNotNull | |
| $users = DB::table('users') | |
| ->whereNull('updated_at') // ->whereNotNull('updated_at') | |
| ->get(); | |
| // whereDate / whereMonth / whereDay / whereYear / whereTime | |
| $users = DB::table('users') | |
| ->whereDate('created_at', '2016-12-31') | |
| // ->whereMonth('created_at', '12') | |
| // ->whereDay('created_at', '31') | |
| // ->whereYear('created_at', '2016') | |
| // ->whereTime('created_at', '=', '11:20:45') | |
| ->get(); | |
| //match column equal or not | |
| $users = DB::table('users') | |
| ->whereColumn('first_name', 'last_name') | |
| ->get(); | |
| ->whereColumn('updated_at', '>', 'created_at') | |
| //multiple where | |
| $users = DB::table('users') | |
| ->whereColumn([ | |
| ['first_name', '=', 'last_name'], | |
| ['updated_at', '>', 'created_at'] | |
| ])->get(); | |
| ****** Orders ******** | |
| $users = DB::table('users') | |
| ->orderBy('name', 'desc') | |
| ->get(); | |
| //latest | |
| $user = DB::table('users') | |
| ->latest() | |
| ->first(); | |
| // Random | |
| $randomUser = DB::table('users') | |
| ->inRandomOrder() | |
| ->first(); | |
| // group by | |
| $users = DB::table('users') | |
| ->groupBy('account_id') | |
| ->groupBy('first_name', 'status') //mmultiple column | |
| ->having('account_id', '>', 100) | |
| ->get(); | |
| ****** Skip/take ***** | |
| $users = DB::table('users')->skip(10)->take(5)->get(); | |
| ***** Limit //offset ****** | |
| $users = DB::table('users') | |
| ->offset(10) | |
| ->limit(5) | |
| ->get(); | |
| ***** Insert ******** | |
| DB::table('users')->insert( | |
| ['email' => '[email protected]', 'votes' => 0] | |
| ); | |
| //insert multiple records | |
| DB::table('users')->insert([ | |
| ['email' => '[email protected]', 'votes' => 0], | |
| ['email' => '[email protected]', 'votes' => 0] | |
| ]); | |
| // get id in return | |
| $id = DB::table('users')->insertGetId( | |
| ['email' => '[email protected]', 'votes' => 0] | |
| ); | |
| ****** Update ******** | |
| DB::table('users') | |
| ->where('id', 1) | |
| ->update(['votes' => 1]); | |
| Updating JSON Columns | |
| DB::table('users') | |
| ->where('id', 1) | |
| ->update(['options->enabled' => true]); | |
| increment & Decrement | |
| DB::table('users')->increment('votes', 1, ['name' => 'John']); | |
| DB::table('users')->increment('votes'); | |
| DB::table('users')->increment('votes', 5); | |
| DB::table('users')->decrement('votes'); | |
| DB::table('users')->decrement('votes', 5); | |
| ********** Delete ******* | |
| DB::table('users')->delete(); | |
| DB::table('users')->where('votes', '>', 100)->delete(); | |
| ********* truncate ****** | |
| //If you wish to truncate the entire table, which will remove all rows and reset the auto-incrementing ID to zero, you may use the truncate method: | |
| DB::table('users')->truncate(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment