正则表达式搜索并用Laravel 4的Fluent或Eloquent替换表中的所有行 [英] Regex search and replace on all rows in a table with Laravel 4's Fluent or Eloquent

查看:62
本文介绍了正则表达式搜索并用Laravel 4的Fluent或Eloquent替换表中的所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Fluent或Eloquent在MySQL数据库中搜索模式并将其替换的最佳方法是什么?

我在Laravel论坛中看到了 能够像这样进行正则表达式选择

// Searching for the word "mypattern"...
Page::where('words', 'regexp', DB::raw('/\bmypattern\b/'))->get();

但是我尝试了这种方法,但似乎没有选择我要的行.无论如何,实际上并不想选择-我实际上想进行搜索并用正则表达式替换.

使用Laravel提供的选项执行此操作的最佳方法是什么?

解决方案

我不认为'regexp'是 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替换表中的所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆