?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Admin extends Model{
//指定表名
protected $table = 'wd_user';
//指定允許批量復(fù)制的字段
protected $fillable = ['username'];
//指定id
protected $primaryKey = 'uid';
//自動維護(hù)時間戳
public $timestamps = true;
//獲取時間格式
protected function getDateFormat()
{
return time();
}
//獲取時間戳
protected function asDateTime($val)
{
return $val;
}
}
public function orm(){
//查詢表的所有記錄
//$user = Admin::all();
//dd($user);
//查詢某一條記錄
//$user = Admin::find(2);
//dd($user);
//findOrFail() 根據(jù)主鍵查找,如果沒有找到就拋出異常
//$user = Admin::findOrFail(1);
//dd($user);
//查詢所有記錄
//$user = Admin::get();
//dd($user);
//增加條件查詢
//$user = Admin::where('uid','>=',4)->orderBy('uid','desc')->first();
//分段查詢
//Admin::chunk(2,function($user){
//var_dump($user);
//});
//聚合函數(shù)
//獲取記錄的條數(shù)
//$count = Admin::count();
//dd($count);
//獲取最大值
//$max = Admin::where('uid','>=',5)->max('age');
//使用模型新增數(shù)據(jù)
//$user = new Admin();
//$user-> username = 'haha';
//$bool = $user->save();
//dd($bool);
//獲取時間
//$user = Admin::find(1);
//echo date('Y-m-d H:i:s',$user->create_at);
//使用模型的Create方法新增數(shù)據(jù)
//$user = Admin::create(['username'=>'meimei']);
//dd($user);
//以屬性查找用戶,如果沒有則新增
//$user = Admin::firstOrCreate(['username'=>'imooc']);
//dd($user);
//以屬性查找用戶,如果沒有則新增,但不保存到數(shù)據(jù)庫
//$user = Admin::firstOrNew(['username'=>'imooc']);
//dd($user);
//通過模型更新數(shù)據(jù)
//$user = Admin::find(1);
//$user->username = 'jack';
//$bool = $user->save();
//dd($bool);
//增加條件
//$num = Admin::where('id','>','1')->update(['age'=>21]);
//dd($num);
//通過模型刪除
//$user = Admin::find(6);
//$bool = $user->delete();
//dd($bool);
//通過主鍵刪除
//$num = Admin::destroy(6);
//dd($num);
//刪除多條記錄
//$num = Admin::destroy(1,2,3);
//$num = Admin::destroy([1,2,3]);
//dd($num);
//增加條件的刪除操作
//$num = Admin::where('uid','>','4')->delete();
//dd($num);
}
更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進(jìn)階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》