将参数(当前选择的 $this->user->id 而不是 auth()->user()->id)从视图传递到 livewire + laravel 中的 createShow
Posted
技术标签:
【中文标题】将参数(当前选择的 $this->user->id 而不是 auth()->user()->id)从视图传递到 livewire + laravel 中的 createShowModal【英文标题】:Passing parameters(current selected $this->user->id and not the auth()->user()->id) from view to createShowModal in livewire + laravel 【发布时间】:2021-07-23 19:30:24 【问题描述】:我想将当前选定的用户 ID 传递给 livewire 中的 showmodal,并且每次尝试单击显示模式按钮时都会收到此错误: enter image description here 如果我删除 $user->id 会出现显示模式,但是当我提交输入的数据时,我得到了同样的错误,例如: enter image description here 这是我的模型数据方法: /** * 此组件中映射的模型的数据 *。
* * @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(), ]);
和模态关系是: 应用\用户
public function appointments()
return $this->hasMany('App\Models\Appointment');
应用\约会
public function user()
return $this->belongsTo('App\Models\User');
请帮忙!
【问题讨论】:
你能分享完整的组件代码和刀片代码吗???您如何检索 $user 并绑定到刀片? 我分享了我的组件 【参考方案1】:如果您通过嵌套组件绑定用户,并且如果通过用户和约会关系是一对多(了解用户有一个或多个约会),我认为必须是这样的:
@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();
【讨论】:
干得好!我使用它进行简单的修改,它的工作原理!非常感谢普洛斯彼罗!! ;)【参考方案2】:这是我的组件代码:
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(),
]);
【讨论】:
我没有发现这个属性 ($this->related_id - $this->user_id) 声明是公开的。当您调用 load 方法为这两个分配值时,$this->user 属性看不到任何初始化,但在刀片中您作为参数(在错误图像中)传递给 createShowModal 方法,并且语句 'related_id' => $this->user->id 与你没有的 $this->user 初始化有关。你能用现在更新的代码修复或更新错误图像吗? 我不擅长 livewire,但我使用同一个用户表并将其声明为 public $user。你的意思是我删除 public $user;并通过 public user_id 更改它;和公开的related_id; 这是在其他刀片中调用它的行:@livewire('user-appointments', ['user' => $user], key($user->id))跨度> 【参考方案3】:这是我的刀片:
<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>
【讨论】:
以上是关于将参数(当前选择的 $this->user->id 而不是 auth()->user()->id)从视图传递到 livewire + laravel 中的 createShow的主要内容,如果未能解决你的问题,请参考以下文章