![1531201901928009.png 814260385-5b42b5412241d_articlex[1].png](http://img.VeVb.com//upload/image/259/207/792/1531201901928009.png)
![1531201908494806.png 2216285623-5b42b5508a1b4_articlex[1].png](http://img.VeVb.com//upload/image/377/401/809/1531201908494806.png)
Active Record 是一种数据访问设计模式,它可以帮助你实现数据对象Object到关系数据库的映射。应用Active Record时,每一个类的实例对象唯一对应一个数据库表的一行(一对一关系)。你只需继承一个abstract Active Record 类就可以使用该设计模式访问数据库,其最大的好处是使用非常简单
![1531201928990874.png 974427235-5b42b55b5a863_articlex[1].png](http://img.VeVb.com//upload/image/961/409/397/1531201928990874.png)
![1531201960341708.png 148572410-5b42b5679400e_articlex[1].png](http://img.VeVb.com//upload/image/540/111/156/1531201960341708.png)
https://github.com/barryvdh/l...
Installation:
composer require barryvdh/laravel-debugbar --dev二、一对一关系映射2.1 创建表
html' target='_blank'>public function up() Schema::create( profiles , function (Blueprint $table) { $table- increments( id $table- string( phone $table- unsignedInteger( user_id //显示的声明外键:通知数据库根据外键关联表和建立索引,提高运行速度 $table- foreign( user_id ) - references( id ) - on( users ) - onDelete( cascade $table- timestamps(); }2.2 创建模型关系2.2.1 正向关系绑定
public function profile() return $this- hasOne(Profile::class);}2.2.2 反向关系绑定
public function user() return $this- belongsTo(User::class);}2.3 外键
![1531201978626122.png 2016152457-5b42c2770c954_articlex[1].png](http://img.VeVb.com//upload/image/711/596/766/1531201978626122.png)
自定义外键:
return $this- hasOne(Profile::class, 显示指定自定义外键2.4 一对一测试
依赖注入Request $request,获取当前登录用户$request- user()
Route::get( /test ,function (Request $request){ //反向// $profile = /App/Profile::find(1);// dd($profile- user); $user = $request- user();// if (is_null($user- profile)){// $user- profile()- create([// phone = 15801340269 // ]); //用firstOrCreate改进if $user- profile()- firstOrCreate([ user_id = $user- id],[ phone = 18363046291 //访问属性一样访问方法 dd($user- profile);});三、一对多关系映射![1531201989311175.png 1821403919-5b42c83248e80_articlex[1].png](http://img.VeVb.com//upload/image/852/674/178/1531201989311175.png)
1:N hasMany(XXX:class) 反之:belongsTo(XXX:class)
![1531202004514341.png 3752176986-5b42c9e330eef_articlex[1].png](http://img.VeVb.com//upload/image/478/430/493/1531202004514341.png)
![1531202040878489.png 3763343682-5b3edff3ec3fb_articlex[1].png](http://img.VeVb.com//upload/image/983/739/297/1531202040878489.png)
![1531202047663554.png 1551414493-5b42d0908f89b_articlex[1].png](http://img.VeVb.com//upload/image/435/793/369/1531202047663554.png)
中间表命名:按照A-Z首字母排序
public function users() return $this- belongsToMany(User::class);public function habits() return $this- belongsToMany(Habit::class);}4.1 面向对象方式绑定多对多的关系
![1531202058946574.png 2030140819-5b42f68d2ebb5_articlex[1].png](http://img.VeVb.com//upload/image/618/821/710/1531202058946574.png)
detach解绑,sync方法用的比较多,只保留1,2![1531202066341134.png 1767484339-5b42f77d7faad_articlex[1].png](http://img.VeVb.com//upload/image/462/758/493/1531202066341134.png)
![1531202076215309.png 1407700575-5b42fa29d7e67_articlex[1].png](http://img.VeVb.com//upload/image/331/331/763/1531202076215309.png)
数据表:
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 * 获得某个国家下所有的用户文章。 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,});![1531202088297717.png 1645904773-5b43054aa5c24_articlex[1].png](http://img.VeVb.com//upload/image/151/422/558/1531202088297717.png)
$factory- define(App/User::class, function (Faker $faker) { return [ name = $faker- name, email = $faker- unique()- safeEmail, country_id = /App/Country::all()- random()- id, password = $2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgpFlYg7B77UdFm , // secret remember_token = str_random(10),});![1531202094256737.png 2472489467-5b430554901f2_articlex[1].png](http://img.VeVb.com//upload/image/852/287/728/1531202094256737.png)
获取每个国家论文总数:
![1531202102778054.png 468658695-5b430428cb53b_articlex[1].png](http://img.VeVb.com//upload/image/983/336/540/1531202102778054.png)
![1531202110449025.png 3328482421-5b430609370ff_articlex[1].png](http://img.VeVb.com//upload/image/143/554/892/1531202110449025.png)
更多:https://laravel-china.org/doc...
伪造数据:![1531202120156807.png 3802634324-5b4309f000d82_articlex[1].png](http://img.VeVb.com//upload/image/395/877/214/1531202120156807.png)
![1531202128696877.png 1929693901-5b4317904c02f_articlex[1].png](http://img.VeVb.com//upload/image/800/113/897/1531202128696877.png)
除了传统的多态关联,您也可以定义「多对多」的多态关联。例如,Post 模型和 Video 模型可以共享一个多态关联至 Tag 模型。 使用多对多多态关联可以让您在文章和视频中共享唯一的标签列表。
更多:https://laravel-china.org/doc...
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP !
相关推荐:
Laravel 5.5中为响应请求提供的可响应接口的详解
关于Laravel基础Migrations的解析
以上就是关于Laravel的Eloquent ORM的解析的详细内容,PHP教程
郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。
新闻热点
疑难解答