phplaravellumenlumen-5.4

Lumen move data of one db table to another db table


I am new to Lumen and Laravel as well. I want to copy the table row from my Post table from Db.post and move the data to Table state in DB.state.

Once that data is moved to state table then delete the data from the Post table (optional).

I am trying to look into database transaction and rollback in Laravel

How do I go about that?

Thanks.


Solution

  • From what I understood you are trying to get the data from one row of the table Post and move it to State table.

    First of all I will assume you are trying to just move the data without any verification. You said about DB Transaction because you may need to delete the older Post entry.

    DB::transaction(function(){
      foreach(Post::all() as $post){
        State::create([
           'first_field' => $post->first_field,
           ...
        ])
    
        $post->delete();
      }
    })