Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ichmag17's avatar

Storing data in multiple pivot tables in Laravel and Livewire

I am working on three tables that have two pivot tables in between. It's a workbook that has a many to many relationship with stations and the same workbook that has a many to many with clients. Right now I have a page where you can save a new workbook and you can select all the stations that should be related to it and also the client that should be associated with that workbook. I figured I would only need to use the store in workbook since that would be the table that connects them both.

public function store(StoreWorkbookRequest $request)
    {
        $workbook = Workbook::create($request->only('wrkb_name'));
        
        $workbook->stations()->sync($request->input('stations', []));
        $workbook->clients()->sync($request->input('clients', []));

        return redirect()->route('admin.workbooks.create')->with('success', 'Successfully Created a New Workbook');
    }

I am also using livewire and I have the form for create in a livewire component

 <form action="{{route('admin.workbooks.store')}}" method="POST" enctype="multipart/form-data">
          @csrf


            <div class="form-group">
                            <label for="">Workbook Name</label>
                            <input type="text" class="form-control" name="wrkb_name">
            </div>


        <table class="table-auto w-full mb-6">
          <thead>
            <tr>
              <th class="px-4 py-2"></th>
              @if($showRate)
              <th wire:click="sortBy('SFM_rate')" style="cursor: pointer;" class="px-4 py-2">SFM Rate @include('partials.sort-icon',['field'=>'SFM_rate'])</th>
                @endif
                @if($showLetter)
              <th wire:click="sortBy('call_letter')" style="cursor: pointer;" class="px-4 py-2">Call Letter @include('partials.sort-icon',['field'=>'call_letter'])</th>
              @endif
              
            </tr>
          </thead>
          <tbody>
            @foreach($stations as $key => $station)
              <tr>
                <td class="border px-4 py-2">
                    <input wire:model="selected" value="{{ $station->id }}" type="checkbox">
                </td>
                @if($showRate)
                <td class="border px-4 py-2">{{$station->SFM_rate}}</td>
                @endif
                @if($showLetter)
                <td class="border px-4 py-2">{{$station->call_letter}}</td>
                @endif
              </tr>
            @endforeach
          </tbody>
        </table>
            {!! $stations->links() !!}
        @else
            <p class="text-center"> No stations were found</p>
        @endif
        <div class="w-full flex pb-10" >
            <div class="w-1/6 relative mx-1">
                <select wire:model="clientselected"  class="block appearance-none w-full bg-gray-200 border border-gray-200 text-gray-700 py-3 px-4 pr-8 rounded leading-tight focus:outline-none focus:bg-white focus:border-gray-500" id="grid-state">
                            <option value="" >Select a Client</option>           
                            @foreach($clients as $id => $client)
                                <option value="{{ $id }}">{{ $client->name }}</option>
                            @endforeach
                </select>
     
            </div>
            <div class="w-1/6 relative mx-1 space-x-6">
                <button class="block appearance-none w-full bg-black border border-gray-200 text-white py-3 px-4 pr-8 rounded leading-tight focus:outline-none focus:bg-white focus:border-gray-500">Create Workbook</button>
            </div>
        </div>
</form>

When I submit the form only the workbook is created and the two pivot tables remain unchanged.

0 likes
4 replies
Tray2's avatar

What does your StoreWorkbookRequest look like?

And what does the request contain?

Looks to me like the

$workbook->stations()->sync($request->input('stations', []));

and the

$workbook->clients()->sync($request->input('clients', []));

Are not sent through properly thus becoming []

ichmag17's avatar

Yes, I actually checked that a few minutes ago and indeed an empty array is getting sent but I am not sure how or where I should look in order to change that.

ichmag17's avatar

This would be the validation basically.

class StoreWorkbookRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'wrkb_name'=> 'required|unique:workbook',
          ];
    }
}
ichmag17's avatar
ichmag17
OP
Best Answer
Level 1

I got it to work. I added in my selectors the name="stations[]" and name="clients[]"

<input  name="stations[]" wire:model="selected" value="{{ $station->id }}" type="checkbox">
<select name="clients[]" wire:model="clientselected"  class="block appearance-none w-full bg-gray-200 border border-gray-200 text-gray-700 py-3 px-4 pr-8 rounded leading-tight focus:outline-none focus:bg-white focus:border-gray-500" id="grid-state">

Now all the data is getting sent.

Please or to participate in this conversation.