【问题标题】:Storing data in multiple pivot tables in Laravel and Livewire在 Laravel 和 Livewire 的多个数据透视表中存储数据
【发布时间】:2021-07-15 18:53:00
【问题描述】:

我正在处理三个表,它们之间有两个数据透视表。这是一个与站点具有多对多关系的工作簿,也是与客户具有多对多关系的同一个工作簿。现在我有一个页面,您可以在其中保存一个新工作簿,您可以选择所有应该与之相关的工作站以及应该与该工作簿相关联的客户端。 我想我只需要在工作簿中使用商店,因为那将是连接它们的表。

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

我也在使用 livewire,并且我有在 livewire 组件中创建的表单。

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

当我提交表单时,只创建了工作簿,两个数据透视表保持不变。

【问题讨论】:

  • 你试过调试吗?也许您的 $request->input() 没有您期望的信息
  • 我对 livewire 不熟悉,你的选择不会还需要 name 属性吗?
  • store方法的开头,放入dd($request-&gt;input('stations'));并给我们输出。它必须是像[1, 2, 3] 这样的数组,如果是别的东西,它对你不起作用。还要确保数组项都是 int 而不是 string.
  • 确实试了调试,好像没有传进去,但是不知道问题出在哪里,数组是空的
  • 在 livewire 中,我可以有一个“已选择”数组,它可以帮助我批量删除电台并且那个有效。我不确定我是否应该使用 livewire 来完成整个商店,尽管这似乎有点复杂,因为我同时拥有 laravel 控制器和 livewire 组件。

标签: laravel pivot-table laravel-livewire


【解决方案1】:

我让它工作了。我在我的选择器中添加了 name="stations[]" 和 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">

现在所有数据都已发送完毕。

【讨论】:

    猜你喜欢
    • 2020-09-01
    • 2021-05-31
    • 2021-08-29
    • 2019-04-07
    • 2020-07-29
    • 2018-04-10
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    相关资源
    最近更新 更多