This article mainly introduces about Laravel eloquent ORM, has a certain reference value, now share to everyone, the need for friends can refer to
First, ORM Programming ideas
1.1 Active Record design mode
Active Record
is a data access design pattern that can help you implement Object
mapping of data objects to relational databases. Active Record
when applied, the instance object of each class uniquely corresponds to one row of a database table (a single relationship). You only have to inherit a abstract Active Record
class to access the database using this design pattern, and the greatest benefit is that it uses very simple
1.2 Debugging Tools Laravel Debugbar
Https://github.com/barryvdh/l ...
Installation:
Composer require Barryvdh/laravel-debugbar--dev
Two, one-to-one relationship mapping
2.1 Creating a Table
Public function up () { schema::create (' Profiles ', function (Blueprint $table) { $table->increments (' Id '); $table->string (' phone '); $table->unsignedinteger (' user_id '); Declared foreign key: Notifies the database to increase the run speed $table->foreign (' user_id ')->references (' id ') ->on (') based on the Foreign Key association table and indexing Users ') ->ondelete (' cascade '); $table->timestamps (); }); }
2.2 Creating a model relationship
2.2.1 Forward Relationship binding
Public Function profile () { return $this->hasone (profile::class);}
2.2.2 Inverse Relationship Binding
Public Function User () { return $this->belongsto (user::class);}
2.3 Foreign Key
Custom FOREIGN Key:
return $this->hasone (Profile::class, ' show specified custom foreign key ');
2.4 One-to-one test
Dependency Injection Request $request
, getting the currently logged on user$request->user()
Route::get ('/test ', function (Request $request) { //reverse// $profile = \app\profile::find (1);// DD ($ Profile->user); $user = $request->user ()// if (Is_null ($user->profile)) {// $user->profile ()->create ([/// ' Phone ' = ' 15801340269 '// ]);/ /}//using firstorcreate to improve if $user->profile () Firstorcreate ([' user_id ' = + $user->id],[ ' phone ' = ' 18363046291 ' ]); Access properties like Access Method DD ($user->profile);});
Three or one-to-many relationship mappings
1:n Hasmany (Xxx:class) Conversely: Belongsto (Xxx:class)
3.1 Object-oriented way to bind one-to-many relationships
Four, many-to-many relationship mappings
Middle table naming: A-Z
Sort by first letter
Public Function users () { return $this->belongstomany (user::class);} Public function habits () { return $this->belongstomany (habit::class);}
4.1 Object-oriented approach to binding many-to-many relationships
Detach, Sync method used more, only to retain the
4.2 Access to many-to-many intermediate data tables
V. Hasmanythrough Object Bridging Traversal Association (far-one-to-many)
Data sheet:
Countries Id-integer name-stringusers id-integer country_id-integer name-stringposts id-integer User_id-integer title-string
Class Country extends model{ protected $fillable = [' name ']; /** * Get all user articles in a country. * /Public Function papers () { return $this->hasmanythrough (paper::class,user::class);} }
$factory->define (App\paper::class, function (Faker $faker) { return [ ' title ' = = $faker->sentence, ' user_id ' = \app\user::all ()->random ()->id, ];});
$factory->define (App\user::class, function (Faker $faker) { return [ ' name ' = = $faker->name, ' Email ' = $faker->unique ()->safeemail, ' country_id ' and \app\country::all ()->random ()->id, ' password ' = ' $2Y$10$TKH8H1.PFQX37YGCZWIKB.KJNYWGAHB9CBCOQGPFLYG7B77UDFM ',//Secret ' Remember_ Token ' = Str_random (Ten), ];});
Get the total number of papers per country:
V. A diversified one-to-many relationship mapping (polymorphic correlation)
Object-oriented polymorphism: Runtime loading mechanism
MORE: Https://laravel-china.org/doc ...
Counterfeit data:
Six, multi-to-many state association
In addition to traditional polymorphic associations, you can also define "many-to-many" polymorphic associations. For example, Post
models and Video
models can share a polymorphic association to a Tag
model. Using a multi-pair association allows you to share a unique list of tags in articles and videos.
MORE: Https://laravel-china.org/doc ...
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!