欢迎光临
感谢一路有你

tp5 数据库操作匿名函数+分页(paginate/each)

如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
 
<?php 

//使用use传入参数
$name = 'thinkphp';
$id = 10;
Db::table('think_user')->where(function ($query) use($name, $id) {
    $query->where('name', $name)
        ->whereOr('id', '>', $id);
})->select();
SELECT * FROM `think_user` WHERE ( `name` = 'thinkphp' OR `id` > 10 )



Db::table('think_user')
->where('name', ['like', 'thinkphp%'], ['like', '%thinkphp'])
->where(function ($query) {
    $query->where('id', ['<', 10], ['>', 100], 'or');
})
->select();
SELECT * FROM `think_user` WHERE ( `name` LIKE 'thinkphp%' AND `name` LIKE '%thinkphp' ) AND ( `id` < 10 or `id` > 100 )




$id=1;
$res = $model->table('channel')
->where('channel_id','NOT IN',function($query) use ($id) {
    $query->table('adv_channel_rule')->where("adv_id",$id)->field('channel_id');
})->select();
SELECT * FROM `channel` WHERE channel_id NOT IN (SELECT channel_id FROM adv_channel_rule WHERE adv_id='.$id.')


// paginate
// each  使用与分页之后进行遍历处理
$list = ProModel::alias('a')
->field('a.*,b.name as brand')
->join('changshang b','a.bid=b.id')
->where($where)
->order('sort desc,id asc')
->paginate($limit,false,['query' => request()->param()])
// ->paginate(10,true)//简介diamante
->each(function($item, $key){
    $item['prices'] = db('product_price')->where(["id"=>$item['id']])->order("quantity asc")->select();
    $item['packaging'] = db('param_child')->where(["id"=>$item['id'],"pid"=>"231"])->value('value');
    $yr = db('changshang')->where(["id"=>$item['bid']])->value('ctime');
    $item['yr'] = date("Y",time())-date("Y",$yr);
    return $item;
});










 

赞(0) 打赏
未经允许不得转载:王明昌博客 » tp5 数据库操作匿名函数+分页(paginate/each)
分享到: 更多 (0)

相关推荐

  • 暂无文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

×
订阅图标按钮