Search code examples
databaseformslaravelcontrollermodels

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 ...


Solution

  • 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 = App\Flight::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.