您好,欢迎访问一九零五行业门户网

Laravel 5 基础(七)- Eloquent (laravel 的ORM)

我们来生成第一个模型php artisan make:model article#输出model created successfully.created migration: 2015_03_28_062517_create_articles_table
查看一下生成的文件 app/article.php
>> $name = 'zhang jinglin';=> zhang jinglin>>> $name=> zhang jinglin>>> $article = new app\article;=> {}>>> $article->title = 'my first article';=> my first article>>> $article->body = 'some content...';=> some content...>>> $article->published_at = carbon\carbon::now();=> { date: 2015-03-28 06:37:22, timezone_type: 3, timezone: utc }>>> $article;=> { title: my first article, body: some content..., published_at: { date: 2015-03-28 06:37:22, timezone_type: 3, timezone: utc } }>>> $article->toarray();=> [ title => my first article, body => some content..., published_at => { date: 2015-03-28 06:37:22, timezone_type: 3, timezone: utc } ]>>> $article->save();=> true#查看数据结果,添加了一条记录>>> app\article::all()->toarray();=> [ [ id => 1, title => my first article, body => some content..., published_at => 2015-03-28 06:37:22, created_at => 2015-03-28 06:38:53, updated_at => 2015-03-28 06:38:53 ] ]>>> $article->title = 'my first update title';=> my first update title>>> $article->save();=> true>>> app\article::all()->toarray();=> [ [ id => 1, title => my first update title, body => some content..., published_at => 2015-03-28 06:37:22, created_at => 2015-03-28 06:38:53, updated_at => 2015-03-28 06:42:03 ] ] >>> $article = app\article::find(1);=> { id: 1, title: my first update title, body: some content..., published_at: 2015-03-28 06:37:22, created_at: 2015-03-28 06:38:53, updated_at: 2015-03-28 06:42:03 }>>> $article = app\article::where('body', 'some content...')->get();=> [ { id: 1, title: my first update title, body: some content..., published_at: 2015-03-28 06:37:22, created_at: 2015-03-28 06:38:53, updated_at: 2015-03-28 06:42:03 } ]>>> $article = app\article::where('body', 'some content...')->first();=> { id: 1, title: my first update title, body: some content..., published_at: 2015-03-28 06:37:22, created_at: 2015-03-28 06:38:53, updated_at: 2015-03-28 06:42:03 }>>> >>> $article = app\article::create(['title' => 'new article', 'body' => 'new body', 'published_at' => carbon\carbon::now()]);illuminate\database\eloquent\massassignmentexception with message 'title'
massassignmentexception,laravel保护我们不能直接插入记录。比如,在一些特殊情况下我们需要直接利用表单的信息填充数据库记录,但是如果我们并没有在表单中添加密码字段,而黑客产生了密码字段连同我们的其他字段一起送回服务器,这将产生修改密码的危险,所以我们必须明确的告诉laravel我们的模型那些字段是可以直接填充的。
修改我们的模型文件 article.php
>> $article = app\article::create(['title' => 'new article', 'body' => 'new body', 'published_at' => carbon\carbon::now()]);=> { title: new article, body: new body, published_at: { date: 2015-03-28 06:55:19, timezone_type: 3, timezone: utc }, updated_at: 2015-03-28 06:55:19, created_at: 2015-03-28 06:55:19, id: 2 } # it's ok>>> app\article::all()->toarray();=> [ [ id => 1, title => my first update title, body => some content..., published_at => 2015-03-28 06:37:22, created_at => 2015-03-28 06:38:53, updated_at => 2015-03-28 06:42:03 ], [ id => 2, title => new article, body => new body, published_at => 2015-03-28 06:55:19, created_at => 2015-03-28 06:55:19, updated_at => 2015-03-28 06:55:19 ] ]>>> $article = app\article::find(2);=> { id: 2, title: new article, body: new body, published_at: 2015-03-28 06:55:19, created_at: 2015-03-28 06:55:19, updated_at: 2015-03-28 06:55:19 }>>> $article->update(['body' => 'new updaet body']);=> true#update自动调用save()
以上就介绍了laravel 5 基础(七)- eloquent (laravel 的orm),包括了方面的内容,希望对php教程有兴趣的朋友有所帮助。
其它类似信息

推荐信息