问题描述
使用Fluent或Eloquent在MySQL数据库中搜索模式并将其替换的最佳方法是什么?
我在Laravel论坛中看到了 能够像这样进行正则表达式选择
// Searching for the word "mypattern"...
Page::where('words', 'regexp', DB::raw('/\bmypattern\b/'))->get();
但是我尝试了这种方法,但似乎没有选择我要的行.无论如何,实际上并不想选择-我实际上想进行搜索并用正则表达式替换.
使用Laravel提供的选项执行此操作的最佳方法是什么?
我不认为'regexp'是有效的查询运算符在Laravel 4中.
Taylor实际上已经在Github上的 issue#838下对此问题发表了评论. ,他建议使用whereRaw代替where.
此处是一个有关MySQL Regex Replace的SO问题,它可以帮助您实现这一点. /p>
What's the best way to use Fluent or Eloquent to search for a pattern in a MySQL database and replace it?
I've seen in the Laravel forums that you should be able to do a regex selection like so
// Searching for the word "mypattern"...
Page::where('words', 'regexp', DB::raw('/\bmypattern\b/'))->get();
But I've tried this method and it doesn't seem to select the rows I'm after. In any case, actually don't want to JUST select--I'd like to actually do a search and replace with regex.
What's the best way to do this with the options provided by Laravel?
I don't believe that 'regexp' is a valid query operator in Laravel 4.
It seems that Taylor has actually made a comment specific to this issue on Github under issue #838, and he suggests using whereRaw instead of where.
Here is an SO question regarding MySQL Regex Replace, which may help you to implement this.
这篇关于正则表达式搜索并用Laravel 4的Fluent或Eloquent替换表中的所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!