Create tables transaction corresponding through a field
I have 4 tables posts, postas, postbs and postcs with the corresponding models Post, Posta, Postb and Postc. They will be related through the groupid field. So in the store method of the controller I have:
// Create a new transaction
$post = new Post;
$post->field1 = $request->field1;
$post->field2 = $request->field2;
$post->save();
$posta = new Posta;
$posta->fielda1 = $request->fielda1;
$posta->fielda2 = $request->fielda2;
$posta->groupid = $post->id;
$posta->save();
$postb = new Postb;
$postb->fieldb1 = $request->fieldb1;
$postb->fieldb2 = $request->fieldb2;
$postb->groupid = $post->id;
$postb->save();
$postc = new Postc;
$postc->fieldc1 = $request->fieldc1;
$postc->fieldc2 = $request->fieldc2;
$postc->groupid = $post->id;
$postc->save();
DB::commit();
// all good
return redirect()->route('posts.index')
->with('success','post was entered with id ' . $post->id);
} catch (\Exception $e) {
DB::rollBack();
return redirect()->route('posts.create')
->with('error', 'Operation failed: ' . $e->getMessage())
->withInput();
}```
Do you think this code is ok?
when creating a model no need to pick fields from request one by one, you can create a model with just one command:
$post = Post::create($request->safe()->only('field1', 'field2')); // assumed you already have validation via form request
$post = Post::create($request->only('field1', 'field2')); // if you leave Request unvalidated
// for subsequent posts depending on first created
$posta = Posta::create([
...$request->only('fielda1', 'fielda2'),
'groupid' => $post->id,
]);
// or using relation:
$posta = new Posta($request->only('fielda1', 'fielda2')):
$posta->group()->associate($post);
$posta->save();
Of course, it is applicable only if field names in a request and a model match.
no need to DB::rollback() in catch block, a transaction is rolled back automatically if any exception was thrown inside. But you may leave it for clarity.
Thank you very much. The associate example helped me a lot!
One last question: My project is written in Laravel 11. Is your example applicable in Laravel 11?
Also another question which has to do with the validated data in case I use a Request: If in the store method, I want to save an extra value in a field what should I do?
Yes, I think Laravel 11 is the same. However I recommend to upgrade to 12 and stick to latest. The longer you stay on outdated version the harder it is to upgrade to actual one.
$pagamData['user_id'] = $request->user()->id;
$pagamData['etoscreated'] = Carbon::now()->year;
$pagamData['unit_id'] = Helper::getCurrentPolitisEnoriaId();
$pagamData['apodektis'] = "Ιερός Ναός";
$pagamData['polit_id'] = auth()->user()->polit->id;
$pagam = Pagam::create($pagamData);```
I found the answer myself. I have to include in the $pagamData validated array the fields user_id, etoscreated etc
By the way talking about upgrading from Laravel 11 to 12 which is the practically easiest way to upgrade my project to 12?
Just follow it step by step on local version (not touching production!). If you face some incompatibilities (PHP version, packages versions etc) you should resolve them and repeat again. Just don't touch production until you have success with local upgrade.