In laravel, the full name of orm is "Object-Relational Mapping", which means "object-relational mapping". Its function is to make a mapping between the relational database and business entity objects; in this way, when operating business objects, There is no need to deal with complex SQL statements, just operate the properties and methods of the object.
The operating environment of this tutorial: Windows 7 system, Laravel 6 version, Dell G3 computer.
What is ORM
ORM, the full name is Object-Relational Mapping, its function is to make a mapping between relational database and business entity objects , In this way, when we operate specific business objects, we no longer need to deal with complex SQL statements, we only need to simply operate the properties and methods of the objects.
ORM implementation method
The two most common implementation methods are ActiveRecord and DataMapper (the former is used in laravel)
Understand the two magic functions __call () and __callStatic ()
class Test{ //动态调用的时候 没有找到此函数 则执行__call() 方法 public function __call($method, $parameters){ echo 22222222222; return (new Rest)->$method(...$parameters); } //静态调用的时候 没有找到此函数 则执行__callStatic()方法 public static function __callStatic($method, $parameters){ echo 1111111111; return (new static)->$method(...$parameters); } } class Rest{ public function foo($name , $age){ echo 333; dump($name,$age); } } //先调用了__callStatic(), 在调用__call(), 然后调用 foo(); Test::foo('张三',17); //只调用了 __call(), 然后调用 foo(); (new Test())->foo('李四',16);die;
/** * Handle dynamic method calls into the model. * * @param string $method * @param array $parameters * @return mixed */public function __call($method, $parameters) { if (in_array($method, ['increment', 'decrement'])) { return $this->$method(...$parameters); } return $this->newQuery()->$method(...$parameters); } /** * Handle dynamic static method calls into the method. * * @param string $method * @param array $parameters * @return mixed */public static function __callStatic($method, $parameters) { return (new static)->$method(...$parameters); }
$list = Politician::where('party_id', 1)->count();
The query constructor in laravel
$list = DB::table('categoty')->get();
laravel video tutorial]
The above is the detailed content of what is laravel orm. For more information, please follow other related articles on the PHP Chinese website!