在学习Eloquent之前首先要了解
php artisan make:model articles
这时你会发现在 app/ 目录下可找到文件 articles.php
(注:php artisan tinker 是 一个 REPL (read-eval-print-loop) ,REPL 是指 交互式命令行界面,它可以让你输入一段代码去执行,并把执行结果直接打印到命令行界面里 )
我们现在开始插入第一条信息(本次实在artisan tinker中调试):
$articles=new App\articles;
$articles->title='first title';
$articles->content='first content';
$articles->published_at=Carbon\Carbon::now();
$articles->intro='first intro';
查看articles
>>> $articles
=> App\articles {#678
title: "next",
content: "content two",
published_at: Carbon\Carbon {#685
+"date": "2017-05-31 11:22:43.000000",
+"timezone_type": 3,
+"timezone": "PRC",
},
}
>>>
此时并没有将数据存储进数据库中,只是给article赋值,写入数据库:
>>> $articles->save();
=> true
>>>
我们可以更清晰的查看刚刚存储进数据库的信息:
>>> $articles->toArray();
=> [
"title" => "next",
"content" => "content two",
"published_at" => Carbon\Carbon {#685
+"date": "2017-05-31 11:22:43.000000",
+"timezone_type": 3,
+"timezone": "PRC",
},
"updated_at" => "2017-05-31 11:29:48",
"created_at" => "2017-05-31 11:29:48",
"intro" => "first intro",
"id" => 2,
]
>>>
有了最简单的创建数据之后我们开始逻辑查找数据:
>>> $my=App\articles::find(2);
=> App\articles {#692
id: 2,
title: "next",
content: "content two",
published_at: "2017-05-31 11:22:43",
created_at: "2017-05-31 11:29:48",
updated_at: "2017-05-31 11:29:48",
intro: "first intro",
}
>>>
更新数据:
>>> $articles->title='update'
=> "update"
>>> $articles->save();
=> true
>>> $articles->find(2)
=> App\articles {#695
id: 2,
title: "update",
content: "content two",
published_at: "2017-05-31 11:36:59",
created_at: "2017-05-31 11:29:48",
updated_at: "2017-05-31 11:36:59",
intro: "first intro",
}
>>>
有时候会用到where查询:
>>> $where_result=App\articles::where('title','=','update')->get();
=> Illuminate\Database\Eloquent\Collection {#702
all: [
App\articles {#705
id: 2,
title: "update",
content: "content two",
published_at: "2017-05-31 11:36:59",
created_at: "2017-05-31 11:29:48",
updated_at: "2017-05-31 11:36:59",
intro: "first intro",
},
],
}
>>>
其中where('title','=','update') '='可以省略,在laravel中为空默认添加'=';
也可以直接返回数据集:
>>> $where_result=App\articles::where('title','update')->first();
=> App\articles {#696
id: 2,
title: "update",
content: "content two",
published_at: "2017-05-31 11:36:59",
created_at: "2017-05-31 11:29:48",
updated_at: "2017-05-31 11:36:59",
intro: "first intro",
}
>>>
我们可以一次性写入数据:
>>> $articles=App\articles::create(['title'=>'third title','content'=>'third con
tent','published_at'=>Carbon\Carbon::now(),'intro'=>'third intro'])
Illuminate\Database\Eloquent\MassAssignmentException with message 'title'
>>>
这是后你会发现报错,这是因为laravel自身默认保护这些字段,不可以直接填充,解决方法:
protected $fillable = ['title','content','published_at','intro'];
然后重新进行写入(注:先退出tinker模式重新进入):
D:\phpStudy\WWW\laravel>php artisan tinker
Psy Shell v0.8.5 (PHP 5.6.27 鈥?cli) by Justin Hileman
>>> $articles=App\articles::create(['title'=>'third title','content'=>'third con
tent','published_at'=>Carbon\Carbon::now(),'intro'=>'third intro'])
=> App\articles {#693
title: "third title",
content: "third content",
published_at: Carbon\Carbon {#689
+"date": "2017-05-31 13:24:41.000000",
+"timezone_type": 3,
+"timezone": "PRC",
},
intro: "third intro",
updated_at: "2017-05-31 13:24:41",
created_at: "2017-05-31 13:24:41",
id: 3,
}
>>>
完美!!!!
数据更新:
>>> $articles->update(['title'=>'third ywp'])
=> true
>>>
create方法对我们来说很便利,一句话就可以搞定数据写入更改;
安装laravel表单扩展参考:https://laravelcollective.com/docs/master/html
setAttribute方法:预处理
public function setTittleAttribute($date){ $this->attributes['published_at'] = Carbon::createFromFormat('Y-m-d',$date); }
quertscopef方法:
控制器中:
public function news(){ DB::table('articles')->published()->get(); }
model:
public function scopePublished($query){ $query->where('published_at','<=',Carbon::now()); }
carbon强大之处
例:
carbon对象->year; 就会显示出时间
carbon对象->diffForhumans();可显示当前时间与设置时间时长;
注:如果对象不是carbon对象时,可在model中设计 $dates=['对象'];