![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教程
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答