上一节,我们通过Laravel的工具:migration ,实现了对数据库进行方便的管理。
现在,了解一下 laravel 项目里面的 model。
我们通过 php artisan 可以查看到相关帮助,找到 model 的相关帮助命令。之前,我们通过 migration 创建了一个articles 表,现在通过命令来生成一下 articles 表的model。
执行命令:php artisan make:model Article
D:\software\wamp64\www\laravel5>php artisan make:model Article Model created successfully.
创建成功!会在 项目根目录 app 文件夹下,生成一个 和 User.php 同级的 Article.php文件。
有了表和modelde 关联,这里演示就可以使用tinker 这个工具!进入命令行监护界面。
D:\software\wamp64\www\laravel5>php artisan tinker Psy Shell v0.9.6 (PHP 7.0.10 ) by Justin Hilemann >>> $name = 'cuikai'; => "cuikai" >>> $name; => "cuikai" >>>
插入数据:
实例化model Article,并赋值:
>>> >>> $articleObj = new App\Article; => App\Article {#2832} >>> >>> $articleObj->title='My title'; => "My title" >>> $articleObj->content = 'My Content'; => "My Content" >>> $articleObj->published_at=Carbon\Carbon::now(); => Carbon\Carbon @1531064532 {#2833 date: 2018-07-08 15:42:12.625854 UTC (+00:00), } >>> $articleObj->intro = 'My intro'; => "My intro" >>> $articleObj; => App\Article {#2832 title: "My title", content: "My Content", published_at: Carbon\Carbon @1531064532 {#2833 date: 2018-07-08 15:42:12.625854 UTC (+00:00), }, intro: "My intro", }
然后执行save(),保存到数据库。执行命令: $articleObj->save();
>>> $articleObj->save(); => true
即可看到我们插入到 articles表的数据插入成功!
如果需要更清晰的看到刚刚插入的那条数据的属性的话,我们可以执行 $articleObj->toArray(); 命令
>>> $articleObj->toArray(); => [ "title" => "My title", "content" => "My Content", "published_at" => Carbon\Carbon @1531064532 {#2833 date: 2018-07-08 15:42:12.625854 UTC (+00:00), }, "intro" => "My intro", "updated_at" => "2018-07-08 15:43:32", "created_at" => "2018-07-08 15:43:32", "id" => 1, ] >>>
查找数据:
通过 find 方法,查找刚才成功插入 id为 1 的数据。执行命令:$firstObj = App\Article::find(1);
>>> $firstObj = App\Article::find(1); => App\Article {#2821 id: 1, title: "My title", content: "My Content", published_at: "2018-07-08 15:42:12", created_at: "2018-07-08 15:43:32", updated_at: "2018-07-08 15:43:32", intro: "My intro", } >>>
更新数据:
我们需要更新某个字段的话,只需要像 之前一样重新赋值,然后再次保存。
>>> $firstObj->title='update'; => "update" >>> $firstObj->save(); => true >>> $firstObj = App\Article::find(1); => App\Article {#108 id: 1, title: "update", content: "My Content", published_at: "2018-07-08 15:42:12", created_at: "2018-07-08 15:43:32", updated_at: "2018-07-08 15:51:21", intro: "My intro", } >>>
查询过程中常用到的where子句:执行命令 App\Article::where('content','=','My Content')->get();
>>> >>> $secondObj = App\Article::where('content','=','My Content')->get(); => Illuminate\Database\Eloquent\Collection {#2819 all: [ App\Article {#2822 id: 1, title: "update", content: "My Content", published_at: "2018-07-08 15:42:12", created_at: "2018-07-08 15:43:32", updated_at: "2018-07-08 15:51:21", intro: "My intro", }, ], } >>> >>> $secondObj = App\Article::where('content','=','My Content')->first(); => App\Article {#2830 id: 1, title: "update", content: "My Content", published_at: "2018-07-08 15:42:12", created_at: "2018-07-08 15:43:32", updated_at: "2018-07-08 15:51:21", intro: "My intro", } >>>
一次性插入数据(非单独赋值,保存),执行命令:
以数组的形式插入数据:$article = App\Article::create(['title'=>'second title','content'=>'second title','published_at'=>Carbon\Carbon::now()]);
>>> $article = App\Article::create(['title'=>'second title','content'=>'second title','published_at'=>Carbon\Carbon::now()]); Illuminate/Database/Eloquent/MassAssignmentException with message 'title'
这个提示由于 laravel 处于保护的原因,默认设置为不可以直接填充,解决这个问题, Article model文件 添加内容:
protected $fillable = ['title','content','published_at','intro'];
退出 tinker,再次进入 tinker, 然后 再次执行上述命令:
>>> $article = App\Article::create(['title'=>'second title','content'=>'second title','published_at'=>Carbon\Carbon::now(),'intro'=>'second intro']); => App\Article {#2834 title: "second title", content: "second title", published_at: Carbon\Carbon @1531066172 {#2839 date: 2018-07-08 16:09:32.804326 UTC (+00:00), }, intro: "second intro", updated_at: "2018-07-08 16:09:32", created_at: "2018-07-08 16:09:32", id: 2, } >>>
更新数据,使用update命令:$article->update(['title'=>'change title']);
>>> $article->update(['title'=>'change title']); => true >>>
删除数据,使用delete命令: $deleteObj->delete();
>>> $deleteObj = App\Article::find(1); => App\Article {#2835 id: 1, title: "update", content: "My Content", published_at: "2018-07-08 15:42:12", created_at: "2018-07-08 15:43:32", updated_at: "2018-07-08 15:51:21", intro: "My intro", } >>> $deleteObj->delete(); => true >>>
更多操作可以查看 官方文档: https://docs.golaravel.com/docs/5.6/eloquent/#deleting-models
本文为崔凯原创文章,转载无需和我联系,但请注明来自冷暖自知一抹茶ckhttp://www.cksite.cn