php - Laravel - 如何替换数据库中的字符串
问题描述
我有一个博客网站。我改变了我的品牌名称。所以我需要更改所有包含我的品牌名称的博客文章。
我可以像这样找到它们:
$search = 'old-brand-name';
$posts = \App\Models\BlogPost::where('description', 'LIKE', '%'.$search.'%')->get();
但是我必须用我的新品牌名称替换数据库级别的这些字符串。我该怎么做?
解决方案
您应该迭代每个帖子,然后通过将 old_brand_name 替换为 new_brand_name 来更改描述并更新此帖子。
foreach($posts as $post){
$old_des = $post->description;
$post->description = str_replace($search,'new-brand-name', $post->description);
$post->save();
}
希望它可以帮助你
推荐阅读
- python - 我想将节点和距离矩阵转换为一个表,其中包含 node1、node2 的列以及它们之间的距离
- matlab - “drawcircle”和“roi.cricle”不尊重预设半径
- java - 如何从 Firestore 中检索纬度和经度并在下面的功能中使用它?
- r - 如何在 R 的 Quanteda 包中应用正则表达式以删除连续重复的标记(单词)
- php - Dart/Flutter 替代 PHP 的 resize_crop_image?
- flutter - 在下拉按钮输入框下显示错误文本
- sql - 如何使用在另一个表中预定义为外键的 id 属性填充表
- java - WFLYEJB0043:定时器的先前执行仍在进行中,跳过此重叠的计划执行
- node.js - 错误:InternalOpenIDError:无法验证断言(Steam)
- sql - 如何在表变量上添加非唯一索引