Home > Software design >  Copy specific column from table to table
Copy specific column from table to table

Time:09-30

Copy specific column from Table Student with column id, name, address, school, age, major to Table A but only column id, name and age

public function student()
{

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

}

but it did not work.

any better suggest

I'm using Laravel 8.6 and MySql.

CodePudding user response:

$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();

Note that you should be referencing the student_id on Table A on the migrations so you should look to your migration file for Table A and add a column called student_id, you can add it as follow

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

CodePudding user response:

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.

In your Transaksi.php file add:

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