[英]Laravel, get data of a specific column from table and store it in a different column name in another table
I have two tables, the first one looks like this:我有两张桌子,第一张看起来像这样:
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');
}
The second one is empty:第二个是空的:
Id Username
Schema::create('join_public', function (Blueprint $table) {
$table->id();
$table->string('username');
$table->timestamps();
}
I want to copy the Name for example (Jack) from the first table and put it in column {Username} in the second table.我想从第一个表中复制名称(例如(Jack))并将其放在第二个表的 {Username} 列中。
It should look like this它应该看起来像这样
Id Username
1 Jack
How can I go about doing so?我怎么能 go 这样做呢?
if you are not using Models.如果您不使用模型。
$user = DB::table("users")->where('name', '=', 'Jack')->first();
DB::table('join_public')->insert(['username' => $user->name]);
if you are using models then如果您使用的是模型,那么
$user = User::where('name', '=', 'Jack')->first();
JoinPublic::create(['username' => $user->name]);
and add this line in model JoinPublic并在 model JoinPublic 中添加这一行
protected $table = 'join_public';
protected $fillable = ['username'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.