简体   繁体   中英

Adding data with multiple tables in one form many to many relationship in laravel 5.4

I am trying to insert with two tables with many to many relationship: Assignments and Collectors. I also make a intermediate or pivot table between them named assignment_collector.

create.blade.php in assignment folder

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

Assignment Model

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

Collector Model

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

I got an error on this: App\\AssignmentCollector since there is no AssignmentCollector and as I read the tutorials, you don't need to make a model for the pivot table. I reading the other articles but I get even more confused. Please help me because I'm still new in Laravel and I have get confused. Thanks!

You don't need a Model for your assignment collector table. What you want to do is create the Assignment then assign

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM