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

时间:2019-11-25 08:55:29

标签: php laravel eloquent

我有一个原始应用程序,其中带有 Registration 表和 Category 表。在RegistrationController中,我使用带有类别的查询,因此我进行了另一个查询。 我遇到的一件事是:检查是否每个其他gender_id都具有一个以0开头的cte_fromcte_gender_id具有1、2或3。1 =男人,2 =女人3 =未定。而且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。

RegistrationController

$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开头,那么它就不会通过。

预先感谢

2 个答案:

答案 0 :(得分:3)

您可以使用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%')

答案 1 :(得分: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");
        }
    }