laravel - 在 Laravel 和 Livewire 中将数据存储在多个数据透视表中
问题描述
我正在处理三个中间有两个数据透视表的表。这是一个与站点具有多对多关系的工作簿,也是与客户具有多对多关系的同一个工作簿。现在我有一个页面,您可以在其中保存一个新工作簿,您可以选择所有应该与之相关的工作站以及应该与该工作簿相关联的客户端。我想我只需要在工作簿中使用商店,因为那将是连接它们的表。
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>
当我提交表单时,仅创建工作簿并且两个数据透视表保持不变。
解决方案
我让它工作。我在我的选择器中添加了 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">
现在所有的数据都被发送了。
推荐阅读
- node.js - 如何使用 node.js 在 sqlite 数据库中的多个条目的值中使用更新方法
- python - 如何使用 zlib 解压缩十六进制字符串?
- mysql - 触发器后的mysql插入使所有插入查询失败
- javascript - 将日期范围划分为已知数量的相等块
- android - 为什么当我使用 Android 导航组件返回片段时总是触发我的 onCreate?
- r - 使用 R 将十六进制字符串转换为 64 位整数/时间戳
- r - 从 HTML 发布到 RPubs 不再是一种选择了吗?
- pandas - pandas 中的大于和小于函数
- enums - Rust 中逻辑上但非法的未初始化变量
- sympy - 收集所有涉及 sympy 因素的术语