首页 > 开发 > 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.  
  9. } 

没什么特别的,除了继承自 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.  
  27. >>> $article; 
  28. => <App/Article #000000005c4b7ee400000000ab91a676> { 
  29. title: "My First Article", 
  30. body: "Some content...", 
  31. published_at: <Carbon/Carbon #000000005c4b7ee600000000ab91dcb6> { 
  32. date: "2015-03-28 06:37:22", 
  33. timezone_type: 3, 
  34. timezone: "UTC" 
  35. } 
  36. } 
  37.  
  38. >>> $article->toArray(); 
  39. => [ 
  40. "title" => "My First Article", 
  41. "body" => "Some content...", 
  42. "published_at" => <Carbon/Carbon #000000005c4b7ee600000000ab91dcb6> { 
  43. date: "2015-03-28 06:37:22", 
  44. timezone_type: 3, 
  45. timezone: "UTC" 
  46. } 
  47. ] 
  48.  
  49. >>> $article->save(); 
  50. => true 
  51.  
  52. #查看数据结果,添加了一条记录 
  53.  
  54. >>> App/Article::all()->toArray(); 
  55. => [ 
  56. [ 
  57. "id" => "1", 
  58. "title" => "My First Article", 
  59. "body" => "Some content...", 
  60. "published_at" => "2015-03-28 06:37:22", 
  61. "created_at" => "2015-03-28 06:38:53", 
  62. "updated_at" => "2015-03-28 06:38:53" 
  63. ] 
  64. ] 
  65.  
  66. >>> $article->title = 'My First Update Title'; 
  67. => "My First Update Title" 
  68.  
  69. >>> $article->save(); 
  70. => true 
  71.  
  72. >>> App/Article::all()->toArray(); 
  73. => [ 
  74. [ 
  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. ] 
  83.  
  84. >>> $article = App/Article::find(1); 
  85. => <App/Article #000000005c4b7e1600000000ab91a676> { 
  86. id: "1", 
  87. title: "My First Update Title", 
  88. body: "Some content...", 
  89. published_at: "2015-03-28 06:37:22", 
  90. created_at: "2015-03-28 06:38:53", 
  91. updated_at: "2015-03-28 06:42:03" 
  92. } 
  93.  
  94. >>> $article = App/Article::where('body', 'Some content...')->get(); 
  95. => <Illuminate/Database/Eloquent/Collection #000000005c4b7e1800000000ab91a676> [ 
  96. <App/Article #000000005c4b7e1b00000000ab91a676> { 
  97. id: "1", 
  98. title: "My First Update Title", 
  99. body: "Some content...", 
  100. published_at: "2015-03-28 06:37:22", 
  101. created_at: "2015-03-28 06:38:53", 
  102. updated_at: "2015-03-28 06:42:03" 
  103. } 
  104. ] 
  105.  
  106. >>> $article = App/Article::where('body', 'Some content...')->first(); 
  107. => <App/Article #000000005c4b7e1900000000ab91a676> { 
  108. id: "1", 
  109. title: "My First Update Title", 
  110. body: "Some content...", 
  111. published_at: "2015-03-28 06:37:22", 
  112. created_at: "2015-03-28 06:38:53", 
  113. updated_at: "2015-03-28 06:42:03" 
  114. } 
  115. >>>  
  116.  
  117. >>> $article = App/Article::create(['title' => 'New Article', 'body' => 'New body', 'published_at' => Carbon/Carbon::now()]); 
  118. 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.  
  13. } 


表示,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.  
  15. # It's ok 
  16.  
  17. >>> App/Article::all()->toArray(); 
  18. => [ 
  19. [ 
  20. "id" => "1", 
  21. "title" => "My First Update Title", 
  22. "body" => "Some content...", 
  23. "published_at" => "2015-03-28 06:37:22", 
  24. "created_at" => "2015-03-28 06:38:53", 
  25. "updated_at" => "2015-03-28 06:42:03" 
  26. ], 
  27. [ 
  28. "id" => "2", 
  29. "title" => "New Article", 
  30. "body" => "New body", 
  31. "published_at" => "2015-03-28 06:55:19", 
  32. "created_at" => "2015-03-28 06:55:19", 
  33. "updated_at" => "2015-03-28 06:55:19" 
  34. ] 
  35. ] 
  36.  
  37. >>> $article = App/Article::find(2); 
  38. => <App/Article #000000005051b22b000000007ec432dd> { 
  39. id: "2", 
  40. title: "New Article", 
  41. body: "New body", 
  42. published_at: "2015-03-28 06:55:19", 
  43. created_at: "2015-03-28 06:55:19", 
  44. updated_at: "2015-03-28 06:55:19" 
  45. } 
  46.  
  47. >>> $article->update(['body' => 'New Updaet Body']); 
  48. => true 
  49.  
  50. #update自动调用save() 


以上所述就是本文的全部内容了,希望能够对大家学习Laravel5框架有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表