当前位置:Gxlcms > PHP教程 > Laravel5框架学习之Eloquent(laravel的ORM)_PHP

Laravel5框架学习之Eloquent(laravel的ORM)_PHP

时间:2021-07-01 10:21:17 帮助过:33人阅读

我们来生成第一个模型

代码如下:


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. use Illuminate\Database\Eloquent\Model;
  3. class Article extends Model {
  4. //
  5. }

没什么特别的,除了继承自 Model 以外,但是具有强大的功能,这些都封装在laravel的Model中。模型自动具有了 save() update() findXXX() 等强大的功能。

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

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

MassAssignmentException,laravel保护我们不能直接插入记录。比如,在一些特殊情况下我们需要直接利用表单的信息填充数据库记录,但是如果我们并没有在表单中添加密码字段,而黑客产生了密码字段连同我们的其他字段一起送回服务器,这将产生修改密码的危险,所以我们必须明确的告诉laravel我们的模型那些字段是可以直接填充的。

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

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

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

退出 tinker,重新进入

  1. >>> $article = App\Article::create(['title' => 'New Article', 'body' => 'New body', 'published_at' => Carbon\Carbon::now()]);
  2. => {
  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. >>> App\Article::all()->toArray();
  16. => [
  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. [
  26. "id" => "2",
  27. "title" => "New Article",
  28. "body" => "New body",
  29. "published_at" => "2015-03-28 06:55:19",
  30. "created_at" => "2015-03-28 06:55:19",
  31. "updated_at" => "2015-03-28 06:55:19"
  32. ]
  33. ]
  34. >>> $article = App\Article::find(2);
  35. => {
  36. id: "2",
  37. title: "New Article",
  38. body: "New body",
  39. published_at: "2015-03-28 06:55:19",
  40. created_at: "2015-03-28 06:55:19",
  41. updated_at: "2015-03-28 06:55:19"
  42. }
  43. >>> $article->update(['body' => 'New Updaet Body']);
  44. => true
  45. #update自动调用save()
  46. </carbon\carbon>

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

人气教程排行