Laravel is a popular PHP framework that provides a powerful and easy-to-use Object-Relational Mapping (ORM) called Eloquent. Eloquent simplifies the process of interacting with databases by allowing developers to work with database records as objects in their code. This makes it easy to query, insert, update, and delete records from a database using PHP code instead of SQL. In this article, we will explore the top 21 Laravel Eloquent methods with example queries.
all():
This method retrieves all records from the database table associated with the model.
Syntax:
ModelName::all();
Parameters: None
Example:
$users = User::all();foreach ($users as $user) { echo $user->name;}
find()
This method retrieves a single record from the database table associated with the model by its primary key.
Syntax:
ModelName::find($id);
Parameters:
$id
– The primary key value of the record to retrieve
Example:
$user = User::find(1);echo $user->name;
findOrFail():
This method retrieves a single record from the database table associated with the model by its primary key, and throws an exception if no record is found.
Syntax:
ModelName::findOrFail($id);
Parameters:
$id
– The primary key value of the record to retrieve
Example:
try { $user = User::findOrFail(100);} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) { echo "User not found";}
where():
This method allows you to add a where clause to your query. You can pass the column name, operator, and value to this method.
Syntax:
ModelName::where($column, $operator, $value)->get();
Parameters:
$column
– The name of the column to filter on$operator
– The operator to use in the where clause (e.g. ‘=’, ‘<>’, ‘>’, ‘<‘, ‘>=’, ‘<=’, ‘like’)$value
– The value to compare against
Example:
$users = User::where('age', '>', 18)->get();foreach ($users as $user) { echo $user->name;}
orWhere():
This method allows you to add an or where clause to your query. You can pass the column name, operator, and value to this method.
Syntax:
ModelName::where($column, $operator, $value)->orWhere($column, $operator, $value)->get();
Parameters:
$column
– The name of the column to filter on$operator
– The operator to use in the where clause (e.g. ‘=’, ‘<>’, ‘>’, ‘<‘, ‘>=’, ‘<=’, ‘like’)$value
– The value to compare against
Example:
$users = User::where('age', '>', 18)->orWhere('name', 'John')->get();foreach ($users as $user) { echo $user->name;}
whereBetween():
This method allows you to add a where clause to your query where the column value is between two values.
Syntax:
ModelName::whereBetween($column, [$value1, $value2])->get();
Parameters:
$column
– The name of the column to filter on$value1
– The lower bound of the range$value2
– The upper bound of the range
Example:
$users = User::whereBetween('age', [18, 30])->get();foreach ($users as $user) { echo $user->name;}
whereNotBetween():
This method allows you to add a where clause to your query where the column value is not between two values.
Syntax:
ModelName::whereNotBetween($column, [$value1, $value2])->get();
Parameters:
$column
– The name of the column to filter on$value1
– The lower bound of the range$value2
– The upper bound of the range
Example:
$users = User::whereNotBetween('age', [18, 30])->get();foreach ($users as $user) { echo $user->name;}
whereIn():
This method allows you to add a where clause to your query where the column value is in a given array of values.
Syntax:
ModelName::whereIn($column, $array)->get();
Parameters:
$column
– The name of the column to filter on$array
– An array of values to match against
Example:
$users = User::whereIn('id', [1, 2, 3])->get();foreach ($users as $user) { echo $user->name;}
whereNotIn():
This method allows you to add a where clause to your query where the column value is not in a given array of values.
Syntax:
ModelName::whereNotIn($column, $array)->get();
Parameters:
$column
– The name of the column to filter on$array
– An array of values to exclude
Example:
$users = User::whereNotIn('id', [1, 2, 3])->get();foreach ($users as $user) { echo $user->name;}
whereNull():
This method allows you to add a where clause to your query where the column value is NULL.
Syntax:
ModelName::whereNull($column)->get();
Parameters:
$column
– The name of the column to filter on
Example:
$users = User::whereNull('deleted_at')->get();foreach ($users as $user) { echo $user->name;}
whereNotNull():
This method allows you to add a where clause to your query where the column value is not NULL.
Syntax:
ModelName::whereNotNull($column)->get();
Parameters:
$column
– The name of the column to filter on
Example:
$users = User::whereNotNull('email')->get();foreach ($users as $user) { echo $user->name;}
whereDate():
This method allows you to add a where clause to your query where the date portion of the column value matches a given date.
Syntax:
ModelName::whereDate($column, $date)->get();
Parameters:
$column
– The name of the column to filter on$date
– The date to match against
Example:
$users = User::whereDate('created_at', '2022-12-31')->get();foreach ($users as $user) { echo $user->name;}
whereMonth():
This method allows you to add a where clause to your query where the month portion of the column value matches a given month.
Syntax:
ModelName::whereMonth($column, $month)->get();
Parameters:
$column
– The name of the column to filter on$month
– The month to match against
Example:
$users = User::whereMonth('created_at', '12')->get();foreach ($users as $user) { echo $user->name;}
whereDay():
This method allows you to add a where clause to your query where the day portion of the column value matches a given day.
Syntax:
ModelName::whereDay($column, $day)->get();
Parameters:
$column
– The name of the column to filter on$day
– The day to match against
Example:
$users = User::whereDay('created_at', '31')->get();foreach ($users as $user) { echo $user->name;}
whereYear():
This method allows you to add a where clause to your query where the year portion of the column value matches a given year.
Syntax:
ModelName::whereYear($column, $year)->get();
Parameters:
$column
– The name of the column to filter on$year
– The year to match against
Example:
$users = User::whereYear('created_at', '2022')->get();foreach ($users as $user) { echo $user->name;}
orderBy():
This method allows you to order the results of your query by a given column.
Syntax:
ModelName::orderBy($column, $direction)->get();
Parameters:
$column
– The name of the column to order by$direction
– The direction to order by (ASC or DESC)
Example:
$users = User::orderBy('name', 'asc')->get();foreach ($users as $user) { echo $user->name;}
Conclusion
Laravel Eloquent provides a powerful and easy-to-use ORM for PHP developers. The top 21 methods covered in this article can help you filter, order, and retrieve data from your database in a variety of ways. By using these methods, you can write more efficient and readable code that interacts with your database records as objects. With its intuitive syntax and extensive documentation, Laravel Eloquent is a powerful tool that can help you build scalable and maintainable applications with ease.
Comments
Post a Comment