【问题标题】:one form submision to 2 controllers in laravel一个表单提交给 laravel 中的 2 个控制器
【发布时间】:2017-07-03 13:56:53
【问题描述】:

所以我的数据库中有 2 个表:示例学生和爱好。 所以这意味着 2 个控制器以及 StudentController 和 HobbyController。 以及 2 个模型。

我有一个表格,例如:

  • 1.学生姓名

  • 2.age

  • 3.高度

  • 4.weight

  • 5.bmi

  • 6.爱好

  • 7.schedule

  • 8.强度

  • 9.饮食

前五个必须去studentcontroller 和 6-9 去 hobbycontroller.. 我是怎么做到的?我不想要两种不同的形式...

【问题讨论】:

  • 您可以使用重定向到action 方法,也可以将该逻辑提取到服务中
  • 感谢您的建议……将提取物提取到服务中

标签: database forms laravel controller models


【解决方案1】:

这可能不是最佳答案,但您可以使用单一表单传递给控制器​​,然后将数据传递给多个存储库。

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

您可以使用任何方法和变量从控制器传递数据。希望这会有所帮助。

编辑:

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

如文档中所述:

The create method returns the saved model instance:

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

有关更多信息,请参阅文档:

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

如果您想将student id 传递给hobby,最简单的方法是从StudentRepository 返回学生并将其传递给HobbyRepository

例如:

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.

您应该将hobbyRepository 存储更改为使用student id

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

【讨论】:

  • 感谢您的努力!这个方法令人兴奋!我从您回答的上一个问题中了解到...我会尝试一下..在此之前我的表单中有一个概率...对于枚举数据类型..我有一个枚举类型的表列。 . 所以当我提交表单时它给了我数据截断错误.. 有什么想法吗?
  • @paladin...修复了枚举数据类型..不用担心兄弟:D
  • 好吧,太好了。 ;) 希望你能尽快找到最好的路径:D
  • btw @ParadiN ....现在如果我想在学生和爱好之间建立关系。这是一对一的关系。我将在哪里分配功能?就像在 laravel 示例中一样,他们在每个模型的控制器中分配它..在我的情况下,我应该在 studentcontroller 中分配它?
  • 如果你想要双向关系,你应该在class Model 上添加关系函数,即:Student.php Hobby.php 如果你想要单向关系,你可以只在任一侧添加. laravel.com/docs/5.3/…You should define the relationship in Model and not in controller.
猜你喜欢
  • 2020-03-05
  • 2021-01-30
  • 1970-01-01
  • 2020-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多