How update Database where, join, update

administrator

Administrator
Nhân viên
9 Tháng tám 2021
68
0
6
Rename user has Nguyen in name to key_names.name by users.key_name_id
Create seeder:

Mã:
<?php

namespace Database\Seeders;
use DB;
use Illuminate\Database\Seeder;

class RenameUserSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */

    public function run()

    {
        DB::table('users')
            ->where('users.name' , ilike, 'Nguyen%')
            ->join('key_names', 'users.key_name_id', '=', 'key_names.id')
            ->update(['users.name' => DB::raw("`key_names`.`name`")]);
    }
}

After run:
Mã:
php artisan db:seed --class=RenameNameSeeder