使用模型新增数据
$student = new Student();
$student->name = 'sean2';
$student->age = '20';
$bool = $student->save();
dd($bool);
$student = Student::find(8);
echo date('Y-m-d H:i:s',$student->created_at);
使用模型的Create方法新增数据
$student = Student::create(
['name' => 'imoc', 'age' => 18]
);
dd($student);
firstOrCreate(查找数据并返回 如果不存在则插入该条数据并返回)
$student = Student::firstOrCreate(
['name' => 'imoocs']
);
firstOrNew(查找数据并返回 如果不存在则返回该类的对象 调用save才得以保存)
$student = Student::firstOrNew(
['name' => 'imoocss']
);
$bool = $student->save();
dd($bool);
指定允许批量赋值字段
protected $fillable = ['name','age']
指定不允许批量赋值的字段
protected $guarded =[]
public $timestaps= true;
protected function getDateFormat()
{
return time()
}
protected function asDataTime($val) {
return $val
}
模型方法的使用
控制器查询数据
模型属性的作用
模型属性和方法
另两种方法
creat增加数据
指定字段赋值
creat增加数据
时间date格式设置
时间戳处理
关闭timestamps
新增数据..
新增....
getDateFormat方法在laravel 5.5版本更新为public
允许批量赋值的字段
getDateFormat()
{ return time();
}
asDateTime($val)
{
return $val;
}
//表名与模型名不相符时手工指定表名
protected $table = '';
//主键字段不叫id时手工指定主键字段
protected $primaryKey = '';
//是否开启自动维护时间戳
public $timestamp = true
自定义created_at updated_at