in laravel i want to insert into two tables the data from one form.
my forms are:
<input type="text" name="name"class="form-control">
<input type="text" name="age"class="form-control">
<input type="text" name="sex"class="form-control">
<input type="text" name="location"class="form-control">
i want to insert the name, age, and sex to table details. and input location to table locations
location model belongsTo detail model.
how to insert them at the same time? and how to automatically match the id of detail with detail_id of location?
i have search but too difficult to follow. i wish there's an easy way to follow.
You didn't show us your model relationships and what you have in your controller so far. But assuming your models are correctly related and your table field names are the same names as your form names, you could try this in your controller:
public function store(Request $request)
{
$detail = new detail();
$detail->name = $request->input("name");
$detail->age = $request->input("age");
$detail->sex = $request->input("sex");
$detail->save();
$location = new location();
$location->detail_id = $detail->id;
$location->location = $request->input("location");
$location->save();
}
hope this help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With