将特定列从 table 复制到 table

Copy specific column from table to table

Table Student 复制特定列,其中包含第 idnameaddressschool , age, major 到 Table A 但只有列 id, nameage

public function student()
{

    Transaksi::create([
        'id_' => $request->id,
        'name' => $request->name,
        'age' => $request->age
    ]);

}

但是没用。

有更好的建议

我正在使用 Laravel 8.6 和 MySql。

$student_column  = StudentModel::where('id', $specific_id)->get()[0];
$table_a_data = new TableAModel;
$table_a_data->student_id = $student_column->id;
$table_a_data->name = $student_column->name;
$table_a_data->age = $student_column->age;
$table_a_data->save();

请注意,您应该在迁移中引用 Table A 上的 student_id,因此您应该查看 Table A 的迁移文件并添加名为 [=14 的列=], 可以按如下添加

$table->unsignedBigInteger('student_id')->nullable();
        $table->foreign('student_id')->references('id')->on('students')->onDelete('cascade');

Mass assignment is a process of sending an array of data that will be saved to the specified model at once. But it may be vulnerable. $fillable property helps us to protect which fields we want to actually allow for creating or updating.

在您的 Transaksi.php 文件中添加:

protected $fillable = ['id', 'name', 'age'];