5

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.

2
  • save to details at first and then location. what's the problem. Commented Jul 13, 2017 at 15:48
  • I have try several way but still cant get it Commented Jul 13, 2017 at 15:50

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks i will try first
It give blank page. Please help.
check your data base, there is no redirect route here that's why it's black page
redirect your page after saving. add this return view(wherever page you want it to be redirected);
How to accepting it? I am sorry iam new to this site
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.