我想在laravel 5.2中使用下面的sql查询
SELECT * FROM `products` WHERE soundex(`products`.`name`)=soundex('Neckholder Creme');
我在这里试过了
return $query->select([ 'products.slug', 'products.id', 'sku', 'name', 'regular_price', 'sale_price', 'sale_from', 'sale_to', 'stock_status', 'product_type', 'featured_image_id' ])
->with('media_featured_image')
->with('categories')
->where('products.product_type', '<>', 'variation')
->where('products.status', 'publish')
->where(function($query) use ($keyword){
foreach($keyword as $k){
$query->where('soundex(products.name)',soundex($k));
}
})
->paginate(120);
但它会给出如下错误,并且由于列名中的而出现问题
Column not found: 1054 Unknown column 'soundex(products.name)' in 'where clause' (SQL: select count(*) as aggregate from `products` where exists (select * from `categories` inner join `category_product` on `categories`.`id` = `category_product`.`category_id` where `category_product`.`product_id` = `products`.`id` and `categories`.`slug` <> shoparchiv) and `products`.`product_type` <> variation and `products`.`status` = publish and (`soundex(products`.`name)` = C352 and `soundex(products`.`name)` = J520))
如何在Laravel中使用?任何帮助都将不胜感激。
谢谢
发布于 2017-02-10 16:07:35
如果您只需要基本查询,那么您可以使用DB::raw
(documentation)
select(DB::raw('SELECT * FROM products WHERE soundex(products.name)=soundex("Neckholder Creme")'));
或者,您可以在eloquent中使用whereRaw
,并在现有查询(documentaion)中使用它。
return $query->select([ 'products.slug', 'products.id', 'sku', 'name', 'regular_price', 'sale_price', 'sale_from', 'sale_to', 'stock_status', 'product_type', 'featured_image_id' ])
->with('media_featured_image')
->with('categories')
->where('products.product_type', '<>', 'variation')
->where('products.status', 'publish')
->where(function($query) use ($keyword){
foreach($keyword as $k){
$query->whereRaw("soundex(products.name) = '".soundex($k)."'");
}
})
->paginate(120);
希望能有所帮助
https://stackoverflow.com/questions/42153706
复制相似问题