jeudi 3 mai 2018

Which is best way about delete row and insert row in Laravel

As title, I have a table like student and another table is class. User can edit about student having some class. There is one to many relationship. When user edit student's class, I always must to delete this student's class. And insert the new class after that.

\App\class:where('student_id',$student->id)->delete();
foreach($request->input('class') as $class){
  $new = new \App\class;
  $new->student_id = $student->id;
  $new->class_id = $class;
  $new->save();
}

Is there have any solution about this issue? Thanks.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire