one form submision to 2 controllers in laravel

so i have 2 tables in my db: example students and hobbies.
so that means 2 controllers as well StudentController and HobbyController.
and as well 2 models.

i have a form where it takes for example:

  • 1.student name
  • 2.age
  • 3.height
  • 4.weight
  • 5.bmi
  • 6.hobby
  • 7.schedule
  • 8.intensity
  • 9.diet

the first five has to go to studentcontroller
and the 6-9 goes to hobbycontroller..
how shud i do this? i dont want 2 different forms …

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

This might not be the best answer but you could use single form to pass to the a controller and then pass the data to the multiple repositories.

route.php

Route::resource('student', 'StudentController');

StudentController.php
public function __constructor(StudentRepository $student, HobbyRepository $hobby)
{
    $this->student = $student;
    $this->hobby= $hobby;
}

public function store(Request $request)
{
    $data = $request->all();
    $hobby = [
        'hobby' => $data['hobby'],
        'schedule' => $data['schedule'],
        'intensity' => $data['intensity'],
        'diet' => $data['diet'],
    ];
    $student = [
        'student_name' => $data['student_name'],
        'age' => $data['age'],
        'height' => $data['height'],
        'weight' => $data['weight'],
        'bmi' => $data['bmi'],
    ];

    $this->student->store($student);
    $this->hobby->store($hobby);

    //your other codes.
}

StudentRepository.php
public function store($data)
{
   // your implementation on storing the user.
}

HobbyRepository.php
public function store($data)
{
   // your implementation on storing the hobby.
}

You could use any method and variables to pass the data from controller. Hope this helps.

Edit:

For your extended question on the storing and retrieving information.

As stated in the docs:

The create method returns the saved model instance:

$flight = AppFlight::create(['name' => 'Flight 10']);

For more information refer to the docs:

https://laravel.com/docs/5.3/eloquent#inserts

If you want to pass the student id to the hobby the simplest method is to return the student from StudentRepository and pass it to the HobbyRepository.

E.g:

StudentRepository.php

public function store($data)
{
   // your implementation on storing the user.
   $student = [] // array of the student informations to be stored.
   return Student::create($student); //you will have student information here.
}

StudentController.php
$student = $this->student->store($student); //store the student information and get the student instance.
$this->hobby->store($hobby, $student->id); //pass it to the hobby to store id.

You should change the hobbyRepository store to use the student id.

This might fix your extended problem.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x