Php 将参数(当前选定的$this->;user->;id,而不是auth()->;user()->;id)从视图传递到livewire+;拉维尔

Php 将参数(当前选定的$this->;user->;id,而不是auth()->;user()->;id)从视图传递到livewire+;拉维尔,php,components,laravel-8,laravel-livewire,Php,Components,Laravel 8,Laravel Livewire,我想将当前选定的用户id传递给livewire中的showmodal,每次尝试单击showmodal按钮时都会出现以下错误: 如果我删除{{$user->id},showmodel会出现,但是当我提交输入的数据时,我得到了相同的错误,如: 这是我的模型数据方法: /***此组件中映射的模型*的数据 * * @return void */ public function modelData() { return [ 'user_id' => auth()-&g

我想将当前选定的用户id传递给livewire中的showmodal,每次尝试单击showmodal按钮时都会出现以下错误: 如果我删除{{$user->id},showmodel会出现,但是当我提交输入的数据时,我得到了相同的错误,如: 这是我的模型数据方法: /***此组件中映射的模型*的数据

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

@if(auth()->user()->role!=“电子医疗保健”)
任命
@恩迪夫
@如果(auth()->user()->role!='Patient')
{{--数据表--}}
病人
电子医疗
治疗
通道丁腈橡胶
地位
@如果($data->count())
@foreach($数据作为$项)
{{--@if($item->related_id==$this->user->id)--}
{{$item->user->name}
{{$item->related_id}
{{$item->treatment}
{{$item->passage_number}
{{$item->status}
@如果(auth()->user()->role=='admin')
@恩迪夫
{{--@endif--}}
@endforeach
@否则
未找到任何结果
@恩迪夫
{{$data->links()}
@恩迪夫
{{--模态形式--}}
@如果($modelId)
{{{(“更新约会”)}
@否则
{{{(添加约会)}
@恩迪夫
--选择一种治疗方法--
@foreach(App\Models\Treatment::all()作为$item)
{{$item->name}
@endforeach
@错误('treatment'){{$message}}@enderror
--选择子处理--
@foreach(App\Models\SubTreatment::all()作为$item)
{{$item->name}
@endforeach
@错误('sub_treatment'){{$message}}@enderror
--选择通道的编号--
@foreach(App\Models\Appointment::passage\u nbr()作为$item)
{{$item}}
@endforeach
@错误('passage_number'){{$message}}@enderror
{{--@if($modelId)--}
--处理--
@foreach(App\Models\Appointment::status()作为$item)
{{$item}}
@endforeach
{{--@endif--}}
@错误('status'){{$message}}@enderror
{{{{('Nevermind')}
@如果($modelId)
{{{('Update')}
@否则
{{{('Create')}
@恩迪夫
{{--删除模式--}}
{{{('Delete Modal Title')}
{{{{('您确定要删除此项目吗?')}
{{{{('Nevermind')}
{{{('Delete Item')}

如果您通过嵌套组件绑定用户,并且如果关系是一个多用户和约会(理解用户有一个或多个约会),我认为应该是这样的:

@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();
}



你能分享完整的组件代码和刀片代码吗???如何检索$user并绑定到blade?我共享了我的组件,但未将此属性($this->related\u id-$this->user\u id)声明作为公共声明。当您调用load method assign values to this,$this->user属性没有看到任何初始化,但在您作为参数(在错误图像中)传递给createShowModal方法的刀片中,语句'related_id'=>$this->user->id与您没有看到的$this->user初始化相关。您能用现在更新的代码修复或更新错误图像吗?我不擅长livewire,但我使用相同的用户表,并将其声明为public$user。你的意思是我删除了public$user;并通过公共用户_id进行更改;与公众有关的身份证;这是在其他刀片中调用它的线路:
@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();
}