将数据存储在 Laravel 和 Livewire 中的多个数据透视表中
Storing data in multiple pivot tables in Laravel and Livewire
我正在研究三个 table,中间有两个枢轴 table。这是一个与站点具有多对多关系的工作簿,以及与客户具有多对多关系的同一工作簿。现在我有一个页面,您可以在其中保存一个新工作簿,您可以 select 所有应该与之相关的站点以及应该与该工作簿相关联的客户端。
我想我只需要使用工作簿中的商店,因为那将是连接它们的 table。
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>
当我提交表单时,只创建了工作簿并且两个数据透视表 table 保持不变。
我让它工作了。我在选择器中添加了 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">
现在正在发送所有数据。
我正在研究三个 table,中间有两个枢轴 table。这是一个与站点具有多对多关系的工作簿,以及与客户具有多对多关系的同一工作簿。现在我有一个页面,您可以在其中保存一个新工作簿,您可以 select 所有应该与之相关的站点以及应该与该工作簿相关联的客户端。 我想我只需要使用工作簿中的商店,因为那将是连接它们的 table。
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>
当我提交表单时,只创建了工作簿并且两个数据透视表 table 保持不变。
我让它工作了。我在选择器中添加了 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">
现在正在发送所有数据。