laravel数据库操作sql语句用Eloquent ORM来构造

时间:2024-06-18 23:05:56

现在有查询语句:

SELECT
users.sNmame,
users.iCreateTime,
users_ext.iAge,
users_ext.sSex
FROM
users
LEFT JOIN users_ext ON users.iAutoId = users_ext.iUserID
WHERE
users.iStatus = 1
AND users_ext.sSex = 0
ORDER BY
users.iCreateTime
LIMIT 0,
1

这里是简单的一个查询语句,接下来就以ORM的形式实现:

public function getUsers(){

        $select = 'users.sNmame,users.iCreateTime,users_ext.iAge,users_ext.sSex';
$resData = UserModel::selectRaw($select)->leftJoin('users_ext','users.iAutoId','=','users_ext.iUserID')->where('users.iStatus','=',1)->where('users_ext.sSex','=',0)->skip(0)->limit(1)->get();
var_dump($resData->toArray());
exit();
}

ok ,以上只是对查询做了一个简单的例子,还有很多需要研究,比如模块之间的关联等等。

装配原生sql语句

        $select = DB::raw('count(settlementAmount) as settlementAmount');
$res = self::where('id','>','1')->select($select)->orderBy('clickTime','desc')->groupBy('groupName')->paginate(20);

转:https://www.cnblogs.com/brudeke/p/4227711.html