leftJoin('category c', 'b.category_id = c.id') ->field('b.*,c.name as category_name') ->when(!empty($categoryID) && $categoryID > 0, function($query) use($categoryID){ $query->where('b.category_id', $categoryID); }) ->when(!empty($name), function($query) use($name){ $query->where('b.name', 'like', "%$name%"); }) ->order('b.id desc') ->paginate([ 'list_rows'=> $size, 'var_page' => 'page', ]); } public static function getByName($name) { $name = trim($name); return self::where('name', $name)->findOrEmpty(); } }