Home >PHP Framework >Laravel >Database Queries and Model Relationships in Laravel: Handling Data Manipulation Elegantly
Database queries and model relationships in Laravel: Handling data operations gracefully
Introduction:
Database operations are an inevitable part of web development, and Laravel As a simple, elegant and flexible development framework, it provides a wealth of database query and model relationship processing tools. Using Laravel, we can perform database operations more conveniently and establish connections between data through model relationships. This article will introduce the method of elegantly processing data operations in Laravel from two aspects: database query and model relationship, to help readers better use Laravel for development.
1. Database query
// Query all users
$users = DB::table('users')->get();
// Query Specify the field and sort by id in descending order
$users = DB::table('users')->select('name', 'email')->orderBy('id', 'desc')-> ;get();
// Query users who are 18 years or older
$users = DB::table('users')->where('age', '>=' , 18)->get();
// Query user data by pagination
$users = DB::table('users')->paginate(10);
namespace AppModels;
use IlluminateDatabaseEloquentModel;
class User extends Model
{
protected $table = 'users';
}
Then you can perform data query and operation through the model:
// Query all users
$users = User ::all();
// Query the specified field and sort by id in descending order
$users = User::select('name', 'email')->orderBy('id', 'desc')->get();
// Query users whose age is greater than or equal to 18 years old
$users = User::where('age', '>=', 18) ->get();
// Query user data by pagination
$users = User::paginate(10);
2. Model relationship
In actual development , there are often certain correlations between data, and Laravel provides a variety of methods to handle the relationship between models, such as one-to-one, one-to-many, many-to-many, etc. The following is an introduction using one-to-many as an example.
hasMany
method in the model class to define a one-to-many association:
namespace AppModels;
use IlluminateDatabaseEloquentModel;
class User extends Model
{
protected $table = 'users'; public function posts() { return $this->hasMany(Post::class); }
}
$user->posts
: $user = User::find(1);
$posts = $ user->posts;
$user = User::find(1);
$posts = $user->posts() ->latest()->limit(5)->get();
$user = User::find(1);
$post = new Post;
$post-> title = 'Hello World';
$post->content = 'Laravel is awesome!';
$user->posts()->save($post);
Conclusion:
Through the database query constructor and model relationship processing tools provided by Laravel, we can more conveniently perform database operations and process the relationship between data during the development process. I hope this article can provide some guidance for readers when using Laravel. Readers are also welcome to explore more of Laravel's elegant methods of data manipulation.
The above is the detailed content of Database Queries and Model Relationships in Laravel: Handling Data Manipulation Elegantly. For more information, please follow other related articles on the PHP Chinese website!