繁体   English   中英

Laravel Query 如何检查列是否以 0 开头

[英]Laravel Query how to check if column starts with 0

我有一个带有RegistrationCategory表的 CRUD 应用程序。 RegistrationController ,我使用了一个带有 Category 的查询,所以我做了另一个查询。 我挣扎检查是否每一个其他gender_id具有cte_from其开始于0。 cte_gender_id具有1,2,或3。1 =人,2 =妇女3 =未确定和Nd cte_from是年龄范围。

因此在数据库中: cte_gender_id为 1, cte_from为 0,然后另一行cte_gender_id为 1, cte_from为 25,然后另一个cte_gender_id为 1 和 35,依此类推,但gender_id 2 和 3。

注册控制器

$gender_ids = ['1', '2', '3'];
foreach ($gender_ids as $gender_id) {
    $category = Categorie::where('cte_distance_id', $distance_id)
        ->where('cte_gender_id', $gender_id)
        ->where('cte_from',)
        ->first();
    
    if (is_null($category)) {
        return back()->with("errorMessage", "Category not found");
    }
}

所以我想要,例如,如果gender_id 是1 并且cte_from 不是从0 开始,它已经没有通过。

您可以使用WHERE cte_from LIKE '0%'来测试字符串是否以0开头。
在此查询中, %用于通配符。

或者在 Laravel 查询构建器中:

->where('cte_from', 'LIKE', '0%')

// Or if it doesn't start with a 0
->where('cte_from', 'NOT LIKE', '0%')

尝试..

->where('cte_from', 'LIKE', '0%')

$gender_ids = ['1', '2', '3'];
    foreach($gender_ids as $gender_id){
        $category = Categorie::where('cte_distance_id', $distance_id)
        ->where('cte_gender_id', $gender_id)
        ->where('cte_from', 'LIKE', '0%')
        ->first();
        if(is_null($category)){
            return back()->with("errorMessage", "Category not found");
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM