[英]Passing parameters(current selected $this->user->id and not the auth()->user()->id) from view to createShowModal in livewire + laravel
我想將當前選定的用戶 ID 傳遞給 livewire 中的 showmodal,並且在每次嘗試單擊顯示模式按鈕時都會出現此錯誤:在此處輸入圖像描述,如果我刪除 {{$user->id}},則顯示模式會出現,但是當我提交了輸入的數據,我得到了同樣的錯誤:在此處輸入圖像描述,這是我的 model 數據方法:/** * model 的數據在此組件中映射 *。
* * @return void */
public function modelData()
{
return [
'user_id' => auth()->user()->id,
'related_id' => $this->user->id,
'treatment' => $this->treatment,
'sub_treatment' => $this->sub_treatment,
'status' => $this->status,
];
}
創建方法:
/** * The create function.
* * @return void */
public function create()
{
$this->validate();
Appointment::create($this->modelData());
$this->modalFormVisible = false; $this->reset();
}
創建顯示模式是:
/** * Shows the create modal
* * @return void */
public function createShowModal()
{
$this->resetValidation();
$this->reset();
$this->modalFormVisible = true;
}
和渲染方法,如:
public function render()
{
return view('livewire.user-appointments', [ 'data' => $this->read(), ]);
}
模態關系為:App\User
public function appointments()
{
return $this->hasMany('App\Models\Appointment');
}
應用\約會
public function user()
{
return $this->belongsTo('App\Models\User');
}
請有任何幫助!
這是我的組件代碼:
public $user;
public $treatment;
public $sub_treatment;
public $passage_number;
public $status;
public $modalFormVisible;
public $modalConfirmDeleteVisible;
public $modelId;
/**
* The validation rules
*
* @return void
*/
public function rules()
{
return [
'treatment' => 'required',
'sub_treatment' => 'required',
'status' => 'required',
'passage_number' => 'required',
];
}
/**
* Loads the model data
* of this component.
*
* @return void
*/
public function loadModel()
{
$data = Appointment::find($this->modelId);
// Assign the variables here
$this->user_id = $data->user_id;
$this->related_id = $data->related_id;
$this->treatment = $data->treatment;
$this->sub_treatment = $data->sub_treatment;
$this->passage_number = $data->passage_number;
$this->status = $data->status;
}
/**
* The data for the model mapped
* in this component.
*
* @return void
*/
public function modelData()
{
return [
'user_id' => auth()->user()->id,
'related_id' => $this->user->id,
'treatment' => $this->treatment,
'sub_treatment' => $this->sub_treatment,
'status' => $this->status,
];
}
/**
* The data for the model mapped
* in this component.
*
* @return void
*/
public function modelDataUpdate()
{
return [
'treatment' => $this->treatment,
'sub_treatment' => $this->sub_treatment,
'status' => $this->status,
'passage_number' => $this->passage_number,
];
}
/**
* The create function.
*
* @return void
*/
public function create()
{
$this->validate();
Appointment::create($this->modelData());
$this->modalFormVisible = false;
$this->reset();
}
/**
* The read function.
*
* @return void
*/
public function read()
{
return Appointment::latest()->with('user')->paginate(5);
}
/**
* The update function
*
* @return void
*/
public function update()
{
$this->validate();
Appointment::find($this->modelId)->update($this->modelDataUpdate());
$this->modalFormVisible = false;
}
/**
* The delete function.
*
* @return void
*/
public function delete()
{
Appointment::destroy($this->modelId);
$this->modalConfirmDeleteVisible = false;
$this->resetPage();
}
/**
* Shows the create modal
*
* @return void
*/
public function createShowModal()
{
$this->resetValidation();
$this->reset();
$this->modalFormVisible = true;
}
/**
* Shows the form modal
* in update mode.
*
* @param mixed $id
* @return void
*/
public function updateShowModal($id)
{
$this->resetValidation();
$this->reset();
$this->modalFormVisible = true;
$this->modelId = $id;
$this->loadModel();
}
/**
* Shows the delete confirmation modal.
*
* @param mixed $id
* @return void
*/
public function deleteShowModal($id)
{
$this->modelId = $id;
$this->modalConfirmDeleteVisible = true;
}
public function render()
{
return view('livewire.user-appointments', [
'data' => $this->read(),
]);
}
這是我的刀片:
<div class="p-6">
@if(auth()->user()->role != 'E-health Care')
<div class="flex items-center justify-end px-4 py-3 text-right sm:px-6">
<a wire:click="createShowModal" class="flex-auto text-center bg-blue-700 text-white py-3 rounded-md text-sm uppercase hover:shadow
hover:bg-blue-500 transform hover:scale-105 motion-reduce:transform-none">
Appoint
</a>
</div>
@endif
@if(auth()->user()->role != 'Patient')
{{-- The data table --}}
<div class="flex flex-col">
<div class="-my-2 overflow-x-auto sm:-mx-6 lg:-mx-8">
<div class="py-2 align-middle inline-block min-w-full sm:px-6 lg:px-8">
<div class="shadow overflow-hidden border-b border-gray-200 sm:rounded-lg">
<table class="min-w-full divide-y divide-gray-200">
<thead>
<tr>
<th class="px-6 py-3 bg-gray-50 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider">Patient</th>
<th class="px-6 py-3 bg-gray-50 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider">E-health Care</th>
<th class="px-6 py-3 bg-gray-50 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider">Treatment</th>
<th class="px-6 py-3 bg-gray-50 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider">Passage Nbr</th>
<th class="px-6 py-3 bg-gray-50 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider">status</th>
<th class="px-6 py-3 bg-gray-50 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider"></th>
</tr>
</thead>
<tbody class="bg-white divide-y divide-gray-200">
@if ($data->count())
@foreach ($data as $item)
{{-- @if($item->related_id == $this->user->id) --}}
<tr>
<td class="px-6 py-2">{{ $item->user->name }}</td>
<td class="px-6 py-2">{{ $item->related_id }}</td>
<td class="px-6 py-2">{{ $item->treatment }}</td>
<td class="px-6 py-2">{{ $item->passage_number }}</td>
<td class="px-6 py-2">{{ $item->status }}</td>
<td class="px-6 py-2 flex justify-end">
<div class="flex space-x-1 justify-around">
<a wire:click="updateShowModal({{ $item->id }})" target="_blank" class="p-1 text-blue-600 hover:bg-blue-600 hover:text-white rounded">
<svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M13.586 3.586a2 2 0 112.828 2.828l-.793.793-2.828-2.828.793-.793zM11.379 5.793L3 14.172V17h2.828l8.38-8.379-2.83-2.828z"></path></svg>
</a>
@if(auth()->user()->role == 'admin')
<button wire:click="deleteShowModal({{ $item->id }})" class="p-1 text-red-600 hover:bg-red-600 hover:text-white rounded">
<svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M9 2a1 1 0 00-.894.553L7.382 4H4a1 1 0 000 2v10a2 2 0 002 2h8a2 2 0 002-2V6a1 1 0 100-2h-3.382l-.724-1.447A1 1 0 0011 2H9zM7 8a1 1 0 012 0v6a1 1 0 11-2 0V8zm5-1a1 1 0 00-1 1v6a1 1 0 102 0V8a1 1 0 00-1-1z" clip-rule="evenodd"></path></svg>
</button>
@endif
</div>
</td>
</tr>
{{-- @endif --}}
@endforeach
@else
<tr>
<td class="px-6 py-4 text-sm whitespace-no-wrap" colspan="4">No Results Found</td>
</tr>
@endif
</tbody>
</table>
</div>
</div>
</div>
</div>
<div class="mt-5">
{{ $data->links() }}
</div>
@endif
{{-- Modal Form --}}
<x-jet-dialog-modal wire:model="modalFormVisible">
@if ($modelId)
<x-slot name="title">
{{ __('Update Appointment') }}
</x-slot>
@else
<x-slot name="title">
{{ __('Add Appointment') }}
</x-slot>
@endif
<x-slot name="content">
<div class="mt-4">
<x-jet-label for="treatment" value="{{ __('Treatment') }}" />
<select wire:model="treatment" id="" class="block appearance-none w-full bg-gray-100 border border-gray-200 text-gray-700 py-3 px-4 pr-8 round leading-tight focus:outline-none focus:bg-white focus:border-gray-500">
<option value="">-- Select a Treatment --</option>
@foreach (App\Models\Treatment::all() as $item)
<option value="{{ $item->name }}">{{ $item->name }}</option>
@endforeach
</select>
@error('treatment') <span class="error">{{ $message }}</span> @enderror
</div>
<div class="mt-4">
<x-jet-label for="sub_treatment" value="{{ __('Sub Treatment') }}" />
<select wire:model="sub_treatment" id="" class="block appearance-none w-full bg-gray-100 border border-gray-200 text-gray-700 py-3 px-4 pr-8 round leading-tight focus:outline-none focus:bg-white focus:border-gray-500">
<option value="">-- Select a Sub Treatment --</option>
@foreach (App\Models\SubTreatment::all() as $item)
<option value="{{ $item->name }}">{{ $item->name }}</option>
@endforeach
</select>
@error('sub_treatment') <span class="error">{{ $message }}</span> @enderror
</div>
<div class="mt-4">
<x-jet-label for="passage_number" value="{{ __('Passage Nbr') }}" />
<select wire:model="passage_number" id="" class="block appearance-none w-full bg-gray-100 border border-gray-200 text-gray-700 py-3 px-4 pr-8 round leading-tight focus:outline-none focus:bg-white focus:border-gray-500">
<option value="">-- Select a nbr of passage --</option>
@foreach (App\Models\Appointment::passage_nbr() as $item)
<option value="{{ $item }}">{{ $item }}</option>
@endforeach
</select>
@error('passage_number') <span class="error">{{ $message }}</span> @enderror
</div>
<div class="mt-4">
<x-jet-label for="status" value="{{ __('Status') }}" />
<select wire:model="status" class="block appearance-none w-full bg-gray-100 border border-gray-200 text-gray-700 py-3 px-4 pr-8 round leading-tight focus:outline-none focus:bg-white focus:border-gray-500">
{{-- @if ($modelId) --}}
<option value="">-- Processing --</option>
@foreach (App\Models\Appointment::status() as $item)
<option value="{{ $item }}">{{ $item }}</option>
@endforeach
{{-- @endif --}}
</select>
@error('status') <span class="error">{{ $message }}</span> @enderror
</div>
</x-slot>
<x-slot name="footer">
<x-jet-secondary-button wire:click="$toggle('modalFormVisible')" wire:loading.attr="disabled">
{{ __('Nevermind') }}
</x-jet-secondary-button>
@if ($modelId)
<x-jet-button class="ml-2" wire:click="update" wire:loading.attr="disabled">
{{ __('Update') }}
</x-jet-danger-button>
@else
<x-jet-button class="ml-2" wire:click="create" wire:loading.attr="disabled">
{{ __('Create') }}
</x-jet-danger-button>
@endif
</x-slot>
</x-jet-dialog-modal>
{{-- The Delete Modal --}}
<x-jet-dialog-modal wire:model="modalConfirmDeleteVisible">
<x-slot name="title">
{{ __('Delete Modal Title') }}
</x-slot>
<x-slot name="content">
{{ __('Are you sure you want to delete this item?') }}
</x-slot>
<x-slot name="footer">
<x-jet-secondary-button wire:click="$toggle('modalConfirmDeleteVisible')" wire:loading.attr="disabled">
{{ __('Nevermind') }}
</x-jet-secondary-button>
<x-jet-danger-button class="ml-2" wire:click="delete" wire:loading.attr="disabled">
{{ __('Delete Item') }}
</x-jet-danger-button>
</x-slot>
</x-jet-dialog-modal>
如果您通過嵌套組件綁定用戶並且如果關系是通過用戶和約會的一對多(了解用戶有一個或多個約會),我認為必須是這樣的:
@livewire('user-appointments', ['user' => $user], key($user->id)) // I assume that this component is for the // appointments of this user
//...in component
public User $user;
public $selectedAppointment;
public $treatment,$sub_treatment,$passage_number,$status;
public $modalFormVisible;
public $modalConfirmDeleteVisible;
public $modelId;
/**
* The validation rules
*
* @return void
*/
public function rules()
{
return [
'treatment' => 'required',
'sub_treatment' => 'required',
'status' => 'required',
'passage_number' => 'required',
];
}
public function render()
{
return view('livewire.user-appointments', [
'data' => $this->read(),
]);
}
/**
* The read function.
*
* @return void
*/
public function read()
{
return $this->user->appointments();
}
/**
* Shows the create modal
*
* @return void
*/
public function createShowModal()
{
$this->modalFormVisible = true;
}
/**
* The create function.
*
* @return void
*/
public function create()
{
$this->validate();
$this->user->appointments()->create($this->modelData());
$this->modalFormVisible = false;
$this->cleanVars();
}
/**
* The data for the model mapped
* in this component.
*
* @return void
*/
public function modelData()
{
return [
'treatment' => $this->treatment,
'sub_treatment' => $this->sub_treatment,
'passage_number' => $this->passage_number,
'status' => $this->status,
];
}
/**
* Shows the form modal
* in update mode.
*
* @param mixed $id
* @return void
*/
public function updateShowModal($id)
{
$this->modelId = $id;
$this->loadModel();
$this->modalFormVisible = true;
}
/**
* Loads the model data
* of this component.
*
* @return void
*/
public function loadModel()
{
$this->selectedAppointment = $this->user->appointments()->where('id', $this->modelId)->first();
// Assign the variables here
$this->treatment = $data->treatment;
$this->sub_treatment = $data->sub_treatment;
$this->passage_number = $data->passage_number;
$this->status = $data->status;
}
/**
* The update function
*
* @return void
*/
public function update()
{
$this->validate();
$this->selectedAppointment->update($this->modelData());
$this->modalFormVisible = false;
$this->cleanVars();
}
/**
* Shows the delete confirmation modal.
*
* @param mixed $id
* @return void
*/
public function deleteShowModal($id)
{
$this->selectedAppointment = $this->user->appointments()->where('id', $id)->first();
$this->modalConfirmDeleteVisible = true;
}
/**
* The delete function.
*
* @return void
*/
public function delete()
{
$this->selectedAppointment->delete();
$this->modalConfirmDeleteVisible = false;
$this-cleanVars();
}
public function cleanVars()
{
$this->treatment = '';
$this->sub_treatment = ''
$this->passage_number = '';
$this->status = '';
$this->modelId = '';
$this->selectedAppointment = '';
$this->resetValidation();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.