Issue
I've 2 create
in the same function store
, but one field of the 1th create
it must be the same of a field in the 2th create.
Is it possible?
DataController.php
public function store(Request $request)
{
SendData::create($request->validated());
// How I take here data from firt ::create ??
$random_data_from_form1 = ??
$data_from_form2 = $request->input('data_form2');
SendMoreData::create([
'field1' => $random_data_from_form1,
'field2' => $data_from_form2
]);
}
Solution
the create()
method with return a new model instance of SendData
.
You can update your code like this:
public function store(Request $request)
{
$new_object = SendData::create($request->validated());
// How I take here data from firt ::create ??
$random_data_from_form1 = $new_object->your_field;
$data_from_form2 = $request->input('data_form2');
SendMoreData::create([
'field1' => $random_data_from_form1,
'field2' => $data_from_form2
]);
}
Answered By - Allen Answer Checked By - Willingham (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.