在Laravel 5.4中以一种形式的多对多关系添加具有多个表的数据

时间:2018-07-11 10:40:03

标签: php laravel laravel-5.4

我试图插入具有多对多关系的两个表:分配和收集器。我还在它们之间创建了一个中间表或数据透视表,命名为Assignment_collector。

在分配文件夹中创建.blade.php

<form method="POST" action="{{ route('assignments.store') }}"> 

        {{ csrf_field() }} 


        <div class="form-group">
            <label for="area_id">Area: </label>
            <select class="form-control" name="area_id">
                @foreach ($areas as $area)
                    <option value= "{{ $area->id }}">
                      {{ $area->campus }} {{ $area->building }} {{ $area->floor }}
                    </option>
                @endforeach
            </select>
        </div>

        <div class="form-group">
            <label for="mts_id">MTS: </label>
            <select class="form-control" name="mts_id">
                @foreach ($mts as $mt)
                    <option value= "{{ $mt->id }}">
                      {{ $mt->location }}
                    </option>
                @endforeach
            </select>
        </div>

        <div class="form-group">
         <input type="hidden" class="form-control" id="status" name= "status" value="Active">
        </div>

        <div class="form-group">
            <label for="collector_id">Collector: </label>
            <select class="form-control" name="collector_id">
                @foreach ($assignments as $assignment)
                    @foreach($assignment->collectors as $collector)
                          <option value= "{{ $collector->id }}">
                            {{ $collector->firstname }} {{ $collector->lastname }}
                          </option>
                    @endforeach

                @endforeach
            </select>
        </div>

        <div class="form-group">
            <button type="submit" class="btn btn-primary">Add</button>
        </div>

    </form>

AssignmentsController

public function store(Request $request)
{
    $this->validate($request, [
        'area_id' => 'required',
        'mts_id' => 'required',
        'status' => 'required',
        'collector_id' => 'required'
    ]);

    $assignment = new Assignment();
    $assignment->area_id = $request->area_id;
    $assignment->mts_id = $request->mts_id;
    $assignment->status = $request->status;
    $assignment->save();

    $assignment_collector = new AssignmentCollector();
    $assignment_collector->collector_id = $request->collector_id;
    $assignment_collector->assignment_id =  $assignment->id;
    $assignment_collector->save();


    return redirect('/assignments');
}

分配模型

public function collectors()
{
    return $this->belongsToMany(Collector::class);
}

收藏家模型

public function assignments()
{
    return $this->belongsToMany(Assignment::class);
}

我遇到了一个错误:App \ AssignmentCollector,因为没有AssignmentCollector,并且在阅读教程时,您不需要为数据透视表创建模型。我读了其他文章,但感到更加困惑。请帮助我,因为我还是Laravel的新手,我感到困惑。谢谢!

1 个答案:

答案 0 :(得分:0)

您的作业收集器表不需要模型。您要做的是创建分配,然后分配

$assignment->collectors()->create([
  // all the collector data
]);