Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel DB::表更新字段_Php_Laravel_Lumen - Fatal编程技术网

Php Laravel DB::表更新字段

Php Laravel DB::表更新字段,php,laravel,lumen,Php,Laravel,Lumen,我有这个疑问 DB::table('pro_orders_has_passengers') ->where('title_name','MR.') ->update([ 'title_name' => 'mr', ]); DB::table('pro_orders_has_passengers') ->where('title_name','MRS.') ->up

我有这个疑问

DB::table('pro_orders_has_passengers')
        ->where('title_name','MR.')
        ->update([
            'title_name' => 'mr',
    ]);
    DB::table('pro_orders_has_passengers')
        ->where('title_name','MRS.')
        ->update([
            'title_name' => 'mrs',
    ]);
    DB::table('pro_orders_has_passengers')
        ->where('title_name','Miss')
        ->update([
            'title_name' => 'ms',
    ]);
    DB::table('pro_orders_has_passengers')
        ->where('title_name','Girl')
        ->update([
            'title_name' => 'girl',
    ]);
    DB::table('pro_orders_has_passengers')
        ->where('title_name','Boy')
        ->update([
            'title_name' => 'boy',
    ]);
我得到了正确的结果。但是我希望在一个查询中得到这个结果。 像这样的

 $titlename = ['MR.','MRS.','Miss','Girl','Boy'];

    DB::table('pro_orders_has_passengers')
        ->where('title_name',$titlename)
        ->update([
            ........
    ]);
我该怎么做?谢谢你的回复

    $titlename = ['mr' => 'MR.','mrs' => 'MRS.','ms' => 'Miss','girl' => 'Girl', 'boy' => 'Boy'];

    $update_query = "UPDATE pro_orders_has_passengers SET title_name = CASE";
    foreach ($titlename  as $new_title => $title) {
       $update_query = $update_query . " WHEN title_name = '$title'  THEN  '$new_title'";
    }
    $update_query = $update_query . " END WHERE title_name IN ('".implode(array_values($titlename), '\',\'')."')";
    \DB::statement($update_query);
这样试试。它将帮助您进行批量更新

最终输出将是

UPDATE pro_orders_已_乘客设置title_name=title_name时的大小写 当title\u name='MR'然后'MR'当title\u name='MRS'然后'MRS'当title\u name='Miss'然后'ms'当title\u name='Girl'然后'Girl'当title\u name='Boy'然后'Boy'时title\u name在哪里结束 (‘先生’、‘太太’、‘小姐’、‘女孩’、‘男孩’)


我想这样你就能实现你想要的

$titlename = ['MR.','MRS.','Miss','Girl','Boy'];
for($i=0; $i < count($titlename);$i++) {
DB::table('pro_orders_has_passengers')
            ->where('title_name',$titlename[$i])
            ->update(['title_name' => strtolower(str_replace('.', '' , $titlename[$i]))]);
}

试试这个。有关更多信息,请参阅以下链接

一,

2.运行一般性声明:


你可以试试<代码>'title\u name'=>\DB::raw(“替换(替换(替换(替换(替换(替换(替换(替换)(替换(title\u name,'MR.,'MRS'),'Miss','ms'),'Girl','Girl'),'Boy','Boy'),'Boy'))”)”对不起,我得到了未定义的偏移量:5l很抱歉,如果使用strotlower无法在MR.MRS之后删除点,但谢谢您检查更新的答案,它将从刚更新的字符串中删除点,如果有任何问题,请重试并粘贴准确的错误消息。SQLSTATE[42000]:语法错误或访问冲突:1064您的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册,以了解在第1行“WHERE title_name IN('MR.,'MRS.,'Miss','Girl','Boy')END'附近使用的正确语法(SQL:UPDATE pro_orders_has_Patients设置title_name=CASE当title_name='MR.'然后'MR'当title_name='MRS.'然后'MRS'当title_name='Miss'然后'ms'当title_name='Girl'然后'Girl'当title_name='Boy'然后'Boy'在title_name中('MR.,'MRS.'Miss','Girl','Boy'))结束)
$titlenames = ['MR.','MRS.','Miss','Girl','Boy'];
foreach($titlenames as $titlename) {
DB::table('pro_orders_has_passengers')
            ->where('title_name',$titlename)
            ->update(['title_name' => strtolower(str_replace('.', '' , $titlename))]);
}
DB::statement(
      UPDATE pro_orders_has_passengers
      SET title_name = CASE  
                    WHEN title_name = 'Mr.' THEN 'mr' 
                    WHEN title_name = 'Mrs.' THEN 'mrs' 
                    //-------Others cases-----//
                    ELSE LASTNAME
                END 
      WHERE title_name IN ('Mr.', 'Mrs.', .....)
);