简体   繁体   English

Laravel,从表中获取特定列的数据并将其存储在另一个表中的不同列名中

[英]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.

相关问题 从表的列中获取数据并存储到另一个表的列 - Get data from column of a table and store to column another table 如何 select 特定列并从这些列中获取数据并将它们存储在另一个表的不同列中? (拉拉维尔) - How to select specific columns and get data from those columns and store them in different columns in another table? (Laravel) 从表的列中插入数据,然后将另一个表存储到列中 - Insert data from column of a table and store to column another table Laravel验证从一列获取值并将其与另一个表列进行比较 - Laravel validation get value from a column and compare it to another table column 将数据从一列添加到另一个表的列Laravel 5.2 - Adding data from a column to another table's column Laravel 5.2 如何在Laravel 5.2中使用不同的数据库表列名进行登录? - How to get login with different database table column name in Laravel 5.2? Laravel:如何在另一个表的所有行中以数组格式获取M:M关系的特定列数据 - Laravel: How to get specific column data of M:M relationship in array format in all row of a another table 如何在laravel5中获取另一个表的列数据? - How to get column data of another table in laravel5? 无法将我的数据存储到具有特定列的表中 - Cannot store my data into table with specific column 如何根据 Laravel 8 中同一表中的另一列在列中存储值 - How to store value in column depending on another column in same table in Laravel 8
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM