| Retrieving Data | all() | Retrieve all records from the database. | $users = User::all(); |
| | find($id) | Retrieve a record by its primary key. | $user = User::find(1); |
| | first() | Retrieve the first record that matches the condition. | $user = User::where('status', 'active')->first(); |
| | get() | Retrieve all records that match the condition. | $users = User::where('status', 'active')->get(); |
| | pluck($column) | Retrieve a single column's value from a collection. | $emails = User::pluck('email'); |
| | value($column) | Retrieve the value of a single column for the first record. | $email = User::where('status', 'active')->value('email'); |
| | findOrFail($id) | Retrieve a record by its primary key or throw an exception if not found. | $user = User::findOrFail(1); |
| Inserting and Updating Data | create($attributes) | Create a new record and save it to the database (mass assignment). | $user = User::create(['name' => 'John', 'email' => 'john@example.com', 'password' => bcrypt('password')]); |
| | update($attributes) | Update existing records. | $user->update(['status' => 'inactive']); |
| | save() | Save a newly created or updated model to the database. | $user->save(); |
| | insert($values) | Insert multiple records into the database. | User::insert([['name' => 'John', 'email' => 'john@example.com']]); |
| Deleting Data | delete() | Delete a record from the database. | $user->delete(); |
| | destroy($id) | Delete a record by its primary key. | User::destroy(1); |
| | forceDelete() | Permanently delete a soft-deleted record. | $user->forceDelete(); |
| Aggregates | count() | Count the number of records matching the condition. | $count = User::where('status', 'active')->count(); |
| | max($column) | Retrieve the maximum value of a column. | $max = User::max('age'); |
| | min($column) | Retrieve the minimum value of a column. | $min = User::min('age'); |
| | avg($column) | Retrieve the average value of a column. | $avg = User::avg('age'); |
| | sum($column) | Retrieve the sum of a column. | $sum = User::sum('age'); |
| Conditions | where($column, $operator, $value) | Add a basic "where" condition. | $users = User::where('status', 'active')->get(); |
| | orWhere($column, $operator, $value) | Add an OR condition. | $users = User::where('status', 'active')->orWhere('role', 'admin')->get(); |
| | whereIn($column, $array) | Add a condition to check if a column value is in an array. | $users = User::whereIn('id', [1, 2, 3])->get(); |
| | whereNull($column) | Add a condition to check if a column is NULL. | $users = User::whereNull('deleted_at')->get(); |
| | whereNotNull($column) | Add a condition to check if a column is not NULL. | $users = User::whereNotNull('updated_at')->get(); |
| Ordering and Sorting | orderBy($column, $direction) | Order the query results by a specific column and direction. | $users = User::orderBy('name', 'asc')->get(); |
| | latest() | Order results by the created_at column in descending order. | $users = User::latest()->get(); |
| | oldest() | Order results by the created_at column in ascending order. | $users = User::oldest()->get(); |
| Grouping Data | groupBy($column) | Group the query results by a specific column. | $grouped = User::groupBy('status')->get(); |
| | having($column, $operator, $value) | Add a condition to filter grouped results. | $grouped = User::select('status', DB::raw('count(*) as user_count'))->groupBy('status')->having('user_count', '>', 10)->get(); |
| Pagination | paginate($perPage) | Retrieve paginated results. | $users = User::paginate(10); |
| | simplePaginate($perPage) | Retrieve paginated results without the complex pagination info. | $users = User::simplePaginate(10); |
| Eloquent Relationships | has($relation) | Retrieve results that have a related model. | $users = User::has('posts')->get(); |
| | with($relation) | Eager load related models. | $users = User::with('posts')->get(); |
| | belongsTo($related) | Define an inverse one-to-many relationship. | public function user() { return $this->belongsTo(User::class); } |
| | hasMany($related) | Define a one-to-many relationship. | public function posts() { return $this->hasMany(Post::class); } |
| | belongsToMany($related) | Define a many-to-many relationship. | public function roles() { return $this->belongsToMany(Role::class); } |