1.简单的数据库插入的方法;
$dbData = ActivityModel::query()->insert($param);
//第二种返回插入对象 ----$dbData
$dbData = (new ConfigHome())->create($param_new);
2.简单的数据库表更新方法:
/**
* 更新数据库如果更新内容一样的话会报失败,一般的话有操作时间,这个时间会变化,所以不会出现问题
*/
$whereArray = array();
$whereArray[] = ['activity_id', '=', $param['activity_id']];
$dbData = ActivityModel::where($whereArray)->update($param);
3.简单的列表查询:
$whereArray[] = ['activity_id', '=', $param['activity_id']];
$whereArray[]=['spu_id', '=', '1323'];
$dbData = ActivityModel::query()->where($whereArray)->get();
4.需要分页的话:
/**
* paginate 不需要添加get来调用
* skip 则需要添加->get()来调用sql
*/
if (isset($param['size'])) {
$pagesize = $param['size'];
}else{
$pagesize = 10;
}
if (isset($param['page'])) {
$page = $param['page'];
} else {
$page = 1;
}
//第一种
->paginate($pagesize,$columns = ['*'], $pageName = 'page', $page);
//第二种
->skip(($page - 1) * $pagesize)->take($pagesize)->where($whereArray)->get();
5.模糊查询:
$whereArray[]=['goods.name', 'like', '%'.'$param['search']'.'%'];
6.排序:
//倒序
->orderBy('activity_id', 'DESC')
//正序
->orderBy('activity_id', 'ASC')
7.多表联查的时候
//多表联查1
->join('goods',function ($join){
$join->on('activity.spu_id', '=', 'goods.goods_id');
},null,null,'left')
//多表联查2
->leftJoin('home_cell', 'home.id', '=', 'home_cell.home_id')
//多表联查3(多条件)
->leftJoin('manage_msg', function ($join) use ($param) {
$join->on('manage_msg.message_id', '=', 'message.id')
->where('manage_msg.user_id', '=', $param['user_id']);
})
8.筛选条件
->when(!empty($whereArray),function($join) use ($whereArray) {
return $join->where($whereArray);
})
->when(!empty($whereInArray),function($join) use ($whereInArray) {
return $join->orWhere($whereInArray);
})
9.查询字段
->select('message.*','manage_msg.create_time as c_time','manage_msg.update_time as u_time','manage_msg.user_id as u_id','manage_msg.message_id','manage_msg.is_read','manage_msg.is_move')