MVC模型
model操作数据库,几种形式:
1、间接在类中定义:
const TABLE_NAME = 'users';protected $table = self::TABLE_NAME;public $timestamps = false;在controller中应用时,就能够间接应用$data = Dvwa::get() -> toArray();
2、第二种获取模式,在model中写入办法,调用数据库,应用get办法时,与其余版本不一样,
function readUser(){
$user_name = DB::table('users')->where('user_id',1)->get(['user','password']); return $user_name;}
在用get获取多个字段时,传入的是一个数组。
在controller中,通过定义对象来进行调用
$dvwa = new Dvwa(); $user = $dvwa -> readUser(); return $user;
获取一列数据应用pluck办法获取:
$user_name = DB::table('users')->pluck('first_name');
此处有个前提是要在env的配置文件中把数据库指定分明: