一个形式submision 2控制器在laravel [英] one form submision to 2 controllers in laravel

查看:163
本文介绍了一个形式submision 2控制器在laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有两个表在我的db:示例学生和爱好。
这样意味着2个控制器以及StudentController和HobbyController。
以及2个模型。



我有一个表格,例如:




  • 1.学生姓名


  • 2.age


  • 3.高度


  • 4.weight


  • 5.bmi


  • 6.hobby


  • 7.schedule

    $ b
  • 8.intensity


  • 9.diet



前五个必须去学生控制
和6-9去hobbycontroller ..
如何shud我这样做?我不想要2种不同的形式...

解决方案

这可能不是最好的答案,但你可以使用单一形式传递a控制器,然后将数据传递到多个存储库。



route.php

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

StudentController.php


$ b b

  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);

//您的其他代码。
}

StudentRepository.php
$ b

  public function store($ data)
{
//你的存储用户的实现。
}

HobbyRepository.php
$ b

  public function store($ data)
{
//你的实现存储爱好。
}

您可以使用任何方法和变量来传递控制器中的数据。希望这有助于。



编辑:



有关存储和检索信息的扩展问题。 >

如文档中所述:

  create方法返回保存的模型实例:

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

有关详细信息,请参阅文档:



< blockquote>

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


如果您要通过学生ID hobby 最简单的方法是从 StudentRepository 返回学生并将其传递给 HobbyRepository



例如:



StudentRepository.php

  public function store($ data)
{
//
$ student = [] //要存储的学生信息数组。
return Student :: create($ student); //你的学生信息在这里。
}

StudentController.php

  $ student = $ this-> student-> store($ student); //存储学生信息并获取学生实例。 
$ this-> hobby-> store($ hobby,$ student-> id); //传递给爱好来存储id。

您应该更改 hobbyRepository 学生ID



这可能会解决您的扩展问题。


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

解决方案

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.

这篇关于一个形式submision 2控制器在laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆