国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

Laravel 5框架學習之Eloquent (laravel 的ORM)

2024-05-04 23:34:03
字體:
來源:轉載
供稿:網友

Laravel 的 Eloquent ORM 提供了漂亮、簡潔的 ActiveRecord 實現來和數據庫的互動。 每個數據庫表會和一個對應的「模型」互動。在開始之前,記得把 config/database.php 里的數據庫連接配置好。

我們來生成第一個模型

復制代碼代碼如下:

php artisan make:model Article

#輸出

Model created successfully.

Created Migration: 2015_03_28_062517_create_articles_table

查看一下生成的文件 app/Article.php

 

 
  1. <?php namespace App; 
  2.  
  3. use Illuminate/Database/Eloquent/Model; 
  4.  
  5. class Article extends Model { 
  6.  
  7. // 
  8.  

沒什么特別的,除了繼承自 Model 以外,但是具有強大的功能,這些都封裝在laravel的Model中。模型自動具有了 save() update() findXXX() 等強大的功能。

tinker 是 laravel提供的命令行工具,可以和項目進行交互。

  1. php artisan tinker 
  2.  
  3. #以下是在tinker中的交互輸入 
  4. Psy Shell v0.4.1 (PHP 5.4.16 — cli) by Justin Hileman 
  5. >>> $name = 'zhang jinglin'
  6. => "zhang jinglin" 
  7.  
  8. >>> $name 
  9. => "zhang jinglin" 
  10.  
  11. >>> $article = new App/Article; 
  12. => <App/Article #000000005c4b7ee400000000ab91a676> {} 
  13.  
  14. >>> $article->title = 'My First Article'
  15. => "My First Article" 
  16.  
  17. >>> $article->body = 'Some content...'
  18. => "Some content..." 
  19.  
  20. >>> $article->published_at = Carbon/Carbon::now(); 
  21. => <Carbon/Carbon #000000005c4b7ee600000000ab91dcb6> { 
  22. date"2015-03-28 06:37:22"
  23. timezone_type: 3, 
  24. timezone: "UTC" 
  25.  
  26. >>> $article
  27. => <App/Article #000000005c4b7ee400000000ab91a676> { 
  28. title: "My First Article"
  29. body: "Some content..."
  30. published_at: <Carbon/Carbon #000000005c4b7ee600000000ab91dcb6> { 
  31. date"2015-03-28 06:37:22"
  32. timezone_type: 3, 
  33. timezone: "UTC" 
  34.  
  35. >>> $article->toArray(); 
  36. => [ 
  37. "title" => "My First Article"
  38. "body" => "Some content..."
  39. "published_at" => <Carbon/Carbon #000000005c4b7ee600000000ab91dcb6> { 
  40. date"2015-03-28 06:37:22"
  41. timezone_type: 3, 
  42. timezone: "UTC" 
  43.  
  44. >>> $article->save(); 
  45. => true 
  46.  
  47. #查看數據結果,添加了一條記錄 
  48.  
  49. >>> App/Article::all()->toArray(); 
  50. => [ 
  51. "id" => "1"
  52. "title" => "My First Article"
  53. "body" => "Some content..."
  54. "published_at" => "2015-03-28 06:37:22"
  55. "created_at" => "2015-03-28 06:38:53"
  56. "updated_at" => "2015-03-28 06:38:53" 
  57.  
  58. >>> $article->title = 'My First Update Title'
  59. => "My First Update Title" 
  60.  
  61. >>> $article->save(); 
  62. => true 
  63.  
  64. >>> App/Article::all()->toArray(); 
  65. => [ 
  66. "id" => "1"
  67. "title" => "My First Update Title"
  68. "body" => "Some content..."
  69. "published_at" => "2015-03-28 06:37:22"
  70. "created_at" => "2015-03-28 06:38:53"
  71. "updated_at" => "2015-03-28 06:42:03" 
  72.  
  73. >>> $article = App/Article::find(1); 
  74. => <App/Article #000000005c4b7e1600000000ab91a676> { 
  75. id: "1"
  76. title: "My First Update Title"
  77. body: "Some content..."
  78. published_at: "2015-03-28 06:37:22"
  79. created_at: "2015-03-28 06:38:53"
  80. updated_at: "2015-03-28 06:42:03" 
  81.  
  82. >>> $article = App/Article::where('body''Some content...')->get(); 
  83. => <Illuminate/Database/Eloquent/Collection #000000005c4b7e1800000000ab91a676> [ 
  84. <App/Article #000000005c4b7e1b00000000ab91a676> { 
  85. id: "1"
  86. title: "My First Update Title"
  87. body: "Some content..."
  88. published_at: "2015-03-28 06:37:22"
  89. created_at: "2015-03-28 06:38:53"
  90. updated_at: "2015-03-28 06:42:03" 
  91.  
  92. >>> $article = App/Article::where('body''Some content...')->first(); 
  93. => <App/Article #000000005c4b7e1900000000ab91a676> { 
  94. id: "1"
  95. title: "My First Update Title"
  96. body: "Some content..."
  97. published_at: "2015-03-28 06:37:22"
  98. created_at: "2015-03-28 06:38:53"
  99. updated_at: "2015-03-28 06:42:03" 
  100. >>>  
  101.  
  102. >>> $article = App/Article::create(['title' => 'New Article''body' => 'New body''published_at' => Carbon/Carbon::now()]); 
  103. Illuminate/Database/Eloquent/MassAssignmentException with message 'title' 


MassAssignmentException,laravel保護我們不能直接插入記錄。比如,在一些特殊情況下我們需要直接利用表單的信息填充數據庫記錄,但是如果我們并沒有在表單中添加密碼字段,而黑客產生了密碼字段連同我們的其他字段一起送回服務器,這將產生修改密碼的危險,所以我們必須明確的告訴laravel我們的模型那些字段是可以直接填充的。

修改我們的模型文件 Article.php

  1. <?php namespace App; 
  2.  
  3. use Illuminate/Database/Eloquent/Model; 
  4.  
  5. class Article extends Model { 
  6.  
  7. protected $fillable = [ 
  8. 'title'
  9. 'body'
  10. 'published_at' 
  11. ]; 
  12.  


表示,title, body, published_at 是可以直接填充的。

退出 tinker,重新進入

  1. >>> $article = App/Article::create(['title' => 'New Article''body' => 'New body''published_at' => Carbon/Carbon::now()]); 
  2. => <App/Article #000000005051b2c7000000007ec432dd> { 
  3. title: "New Article"
  4. body: "New body"
  5. published_at: <Carbon/Carbon #000000005051b2c6000000007ec4081d> { 
  6. date"2015-03-28 06:55:19"
  7. timezone_type: 3, 
  8. timezone: "UTC" 
  9. }, 
  10. updated_at: "2015-03-28 06:55:19"
  11. created_at: "2015-03-28 06:55:19"
  12. id: 2 
  13.  
  14. # It's ok 
  15.  
  16. >>> App/Article::all()->toArray(); 
  17. => [ 
  18. "id" => "1"
  19. "title" => "My First Update Title"
  20. "body" => "Some content..."
  21. "published_at" => "2015-03-28 06:37:22"
  22. "created_at" => "2015-03-28 06:38:53"
  23. "updated_at" => "2015-03-28 06:42:03" 
  24. ], 
  25. "id" => "2"
  26. "title" => "New Article"
  27. "body" => "New body"
  28. "published_at" => "2015-03-28 06:55:19"
  29. "created_at" => "2015-03-28 06:55:19"
  30. "updated_at" => "2015-03-28 06:55:19" 
  31.  
  32. >>> $article = App/Article::find(2); 
  33. => <App/Article #000000005051b22b000000007ec432dd> { 
  34. id: "2"
  35. title: "New Article"
  36. body: "New body"
  37. published_at: "2015-03-28 06:55:19"
  38. created_at: "2015-03-28 06:55:19"
  39. updated_at: "2015-03-28 06:55:19" 
  40.  
  41. >>> $article->update(['body' => 'New Updaet Body']); 
  42. => true 
  43.  
  44. #update自動調用save() 


以上所述就是本文的全部內容了,希望能夠對大家學習Laravel5框架有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沙田区| 石柱| 龙海市| 乃东县| 神池县| 板桥市| 高邑县| 威海市| 凤翔县| 尚义县| 四平市| 修水县| 清丰县| 桓台县| 磴口县| 门源| 屏东县| 八宿县| 黄冈市| 土默特左旗| 新宾| 伊宁市| 东乌| 盖州市| 昌乐县| 胶州市| 广东省| 兰考县| 丰都县| 陇西县| 九龙坡区| 永清县| 梁河县| 文昌市| 平泉县| 潍坊市| 兴文县| 宜兰市| 米脂县| 福贡县| 潍坊市|