2016-09-29 109 views
2

我写我的工作台sqlquery的第一个其实之前把它移到laravel代码,我可以证实这工作。这里是我的SQL查询写在工作台Laravel whereRaw不返回任何东西

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

+0

检查laravel日志和mysql日志。说实话,我没有看到你的查询有任何明显的错误。唯一不合适的是'COLLATE utf8_unicode_ci'末尾的';',你不需要这样做。 – Andrew

+0

将' - > get'替换为' - > toSql()'转储此值并与您的预期进行比较,并将其添加到此问题中,如果您需要更多帮助。 –

回答

0

尝试从你的“其中”语句删除分号,如果这被插入通过Laravel生成的查询,则很可能无法在所有运行它。

0

我觉得认为是没有问题的有它在你的模型。

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 
    } 
}