Laravel Eloquent Orm 使用指南
1、描述1vs1关系
例如比如又两张表,一张部门表,一张经理表,一个部门只有一个经理,一个经理也只控制一个部门,就表达了一对一的关系
class User extends Eloquent {
//一对一
public function dept() {
return $this->hasOne('Dept'); // this matches the Eloquent model
}
class Dept extends Eloquent {
public function user() {
return $this->belongsTo('user'); // this matches the Eloquent model
}
我们可以总结出,在一对一关系中,主表(也就是没有保存user_id表的那个表),用hasOne表示,另外一个表,保存了user_id的,用belongsTo来表述。换句话说,当我们看到一个表有blongsTo的时候,如果后面没有带参数,也就代表着这个表利用方法名_id来跟前面的表关联
2、描述1vs多关系
例如班级表,学生表,一个班级有很多名学生,一个学生只在一个班级,因此class表跟student表是一对多关系
class Squad extends Eloquent {
//一对多
public function student() {
return $this->hasMany('Student');
}
class Student extends Eloquent {
public function squad() {
return $this->belongsTo('Squad');
}
我们可以总结出,在一对多关系中,主表(也就是没有保存squad_id表的那个表),用hasMany表示,另外一个表,保存了squad_id的,用 belongsTo来表述。
3、描述多对多关系
例如角色表,用户表,一个角色有多个用户,一个用户可以拥有多个角色
Class Role extentds Eloquent{
public function User{
return $this->belongsToMany('Role','user_role');
}
}
PS:其实我们并非一定要在我们的模型中写这么多可能另我们自己都费解的东西,只是根据需要我们需要进行某些查询的时候才写,举个简单例子,如果我们只需要查一个角色有多少个用户,而我们发现角色跟用户是多对多关系,我们就可以在Role表中写个User方法。如果我们需要查一个班级的所有学生,我们发现班级跟学生是一对多关系,我们就可以在班级模型中写Student方法,用到hasMany。
我们没有必要为这些东西引乱了我们的思维,一切根据实际需求来,需要查询的时候,再想关系,再写方法。例如我要查某个角色的用户,我们心中要清楚角色跟用户是多多对关系,自然而然就会用到blongsToMany,而不是我们凭空提前就去捏造。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











There are four main error types in PHP: 1.Notice: the slightest, will not interrupt the program, such as accessing undefined variables; 2. Warning: serious than Notice, will not terminate the program, such as containing no files; 3. FatalError: the most serious, will terminate the program, such as calling no function; 4. ParseError: syntax error, will prevent the program from being executed, such as forgetting to add the end tag.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.
