【问题标题】:how to store data in multiple tables from single form in laravel如何在laravel中从单一形式将数据存储在多个表中
【发布时间】:2019-12-01 20:52:15
【问题描述】:

嗨,我想将数据从一种形式存储到 2 个 db 表中,但无法做到:在我的代码中,我希望教师可以添加多个学生,,教师数据保存到教师表中,学生数据保存到学生表中使用我的数据库表的教师 ID 屏幕截图:https://ibb.co/ctr7FDPhttps://ibb.co/nwmcnPX 这是视图https://ibb.co/CBytRCR 根据此视图,我想在学生表中插入多个学生姓名及其教师 ID,任何指导线和注意, ,, m 为它使用资源路由

教师控制器代码:

  public function store(Request $request)
{
    $request->validate([
        'teacher_name'    =>  'required',
    ]);

     $teacher = new Teacher;
     $teacher->teacher_name = $request->teacher_name;
     $teacher->save();

     return redirect('teacher');
}

刀片文件:

    <form method="POST" action="{{ route('teacher.store') }}">
  @csrf
  <div class="form-group">
    <label for="">Teacher Name</label>
    <input type="text" name="teacher_name" class="form-control input {{ $errors->has('teacher_name') ? 'is-danger' : ''}}" id="" placeholder="Teacher Name">
  </div>

  <div class="form-group">
    <label class="text-right">Enter Student Name</label>
    <div class="col-md-8">
      <div class="wrapper">
        <div><input type="text" name="student_name[]" class="form-control input-lg" placeholder="Enter Student Name" /></div>
        </div>
        <p><button class="add_fields">Add More Students</button></p>
    </div>
  </div>

  <input type="submit" name="submit" class="btn btn-primary" value="submit">

  @include('errors')

</form>

教师模型:

  public function student()
{
    $this->hasMany('App\Student');
}

学生模型:

public function teacher()
{
    return $this->belongsTo('App\Teacher');
}

【问题讨论】:

    标签: laravel eloquent laravel-5.8


    【解决方案1】:
    public function students()   //student to students
    {
        $this->hasMany('App\Student');
    }
    

    插入代码应该是

    $teacher->students()->createMany([[$student1],[$student2]]);
    

    【讨论】:

    • 现在它说 Call to a member function createMany() on null 我在教师的控制器存储函数中编写了这段代码
    • 你把学生改成学生了吗?
    【解决方案2】:

    假设您要从名为 student_id 的变量中的表单中引入多个学生,您可以在保存 teacher 后使用 Laravel 的 attach() 方法附加他们。

    $teacher->student()->attach($request->get('student_id'));
    

    由于您有很多学生到老师,我建议您将 Teacher 模态上的方法重命名为复数 (students)。

    不过,查看您的表格,如果这是您的 FK,我建议您捕获 student_id[] 而不是名称 - 让 name 来自学生领域,但为了连接,你需要数组中的ids,而不是name

    【讨论】:

    • 您的表单看起来需要一些工作 - 我看不到您将 ids/names 的值存储在哪里。以上对你有用,但我认为你需要注意表单和发送给它的数据。
    • 这个公共函数students()它说调用未定义的方法App\Teacher::student(),然后它说调用一个成员函数attach() on null
    • 是的,如果更改关系的名称,请在上面的代码中更改关系方法的名称。因此,如果您的 Teacher 模型的关系称为 students(),那么上面的代码现在将变为:$teacher-&gt;students()-&gt;attach($request-&gt;get('student_id'));。如果您不按照我在答案中的建议添加一些内容并进行一些更改,那么这一切都不会奏效。如果您从名为student_ids 的数组中的表单中返回一组学生的 id,并且您将这些 id 从您的控制器正确发送到表单,这将满足您的要求。不过,我会做一些工作。
    • 我把它编辑到这个 foreach ($request->student_name as $name) { $student->student_name = $request->student_name; $学生->保存(); } 但现在它说从空值创建默认对象
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-24
    • 2019-04-16
    • 1970-01-01
    • 2013-05-27
    • 2019-02-26
    相关资源
    最近更新 更多