繁体   English   中英

Laravel whereRaw不返回任何东西

[英]Laravel whereRaw not returning anything

我先在工作台中编写了sqlquery,然后才将其移至laravel代码,我可以确认这项工作。 这是我在工作台中编写的SQL查询

SET @info = 'hello';

SELECT device_type_id 
FROM arecibo.device_type_mappings
WHERE @info LIKE concat('%', name , '%') COLLATE utf8_unicode_ci;

我现在想将其转换为代码,但是由于没有返回任何内容而遇到了麻烦。 我四处搜寻以查看可能缺少的内容,但无济于事,我找不到任何东西。 这是我尝试的代码:

$deviceTypeId = DB::table('device_type_mappings')
        ->select('device_type_id')
        ->whereRaw('? LIKE concat(\'%\', name , \'%\') COLLATE utf8_unicode_ci;', [$sysInfo])
        ->get();

如果可能的话,我想使用该模型,但是它抱怨没有公开选择方法。

我正在使用laravel 5.3

尝试从“ where”语句中删除分号,如果该语句已插入Laravel生成的查询中,则它可能根本无法运行。

我认为在您的Model上使用它没有问题。

class Any extends Model
{
    protected $table = 'device_type_mappings';

    public static function getSomething()
    {
        $column = 'info';

        $a = self::whereRaw('? LIKE CONCAT(\'%\', name, \'%\') COLLATE utf8_unicode_ci', [$column])->select('device_type_id');

        die($a->toSql());

        //OUTPUT: select `device_type_id` from `device_type_mappings` where ? LIKE CONCAT('%', name, '%') COLLATE utf8_unicode_ci
    }
}

暂无
暂无

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

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