簡體   English   中英

Laravel,從表中獲取特定列的數據並將其存儲在另一個表中的不同列名中

[英]Laravel, get data of a specific column from table and store it in a different column name in another table

我有兩張桌子,第一張看起來像這樣:

Id   Name   Email           Password
1    Jack   Jack@gmail.com  Ksecneidsw54?

Schema::create('users', function (Blueprint $table) {
                $table->id();
                $table->string('name');
                $table->string('email')->unique();
                $table->string('password');
            }

第二個是空的:

Id   Username

Schema::create('join_public', function (Blueprint $table) {
            $table->id();
            $table->string('username');
            $table->timestamps();
        }

我想從第一個表中復制名稱(例如(Jack))並將其放在第二個表的 {Username} 列中。

它應該看起來像這樣

Id   Username
1    Jack

我怎么能 go 這樣做呢?

如果您不使用模型。

$user = DB::table("users")->where('name', '=', 'Jack')->first();
DB::table('join_public')->insert(['username' => $user->name]);

如果您使用的是模型,那么

$user = User::where('name', '=', 'Jack')->first();
JoinPublic::create(['username' => $user->name]);

並在 model JoinPublic 中添加這一行

protected $table = 'join_public';
protected $fillable = ['username'];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM