tp5怎么關聯模型進行條件查詢?


 

// 查詢用戶昵稱是think的用戶

// 注意第一個參數是關聯方法名(不是關聯模型名)

$users = User::hasWhere('profile', ['nickname'=>'think'])->select();

 

// 可以使用閉包查詢

$users = User::hasWhere('profile', function($query) {

    $query->where('nickname', 'like', 'think%');

})->select();

 

模型里面

    public function authGroupAccess()

    {

        return $this->belongsTo('authGroupAccess''id''uid')->bind('group_id');

    }

 

控制器里面:

hasWhere的使用

   

if (!empty($param['group_id'])) {

            $haswhere['group_id'= $param['group_id'];

}

 

 

$list = model('admin')

            ->hasWhere('authGroupAccess', $haswhere)

            ->with('authGroupAccess,authGroup')

            ->where('username''neq''admin')

            ->order('id','desc')

            ->where($where)

            ->paginate(config('page_number'));

 

 

文章來源:劉俊濤的博客 歡迎關注公眾號、留言、評論,一起學習。

 

若有幫助到您,歡迎點擊推薦,您的支持是對我堅持最好的肯定(*^_^*)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM