Search code examples
phpcomponentslaravel-8laravel-livewire

Passing parameters(current selected $this->user->id and not the auth()->user()->id) from view to createShowModal in livewire + laravel


I want to pass the current selected user id to showmodal in livewire and in every attempt when I click to showmodal button I get this error: enter image description here and if I delete {{$user->id}} the showmodal appears but when I submit the entered data I got the same error like: enter image description here and this is my model data method: /** * 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 create method:

/** * The create function. 
* * @return void */ 
public function create() 
{ 
     $this->validate(); 
     Appointment::create($this->modelData()); 
     $this->modalFormVisible = false; $this->reset(); 
}

the create show modal is:

/** * Shows the create modal 
* * @return void */ 
public function createShowModal()  
{ 
      $this->resetValidation(); 
      $this->reset(); 
      $this->modalFormVisible = true; 
}

and the render method like:

public function render() 
{ 
     return view('livewire.user-appointments', [ 'data' => $this->read(), ]); 
}

and the modal relationship is: App\User

public function appointments() 
{ 
     return $this->hasMany('App\Models\Appointment'); 
}

App\Appointment

public function user() 
{ 
     return $this->belongsTo('App\Models\User'); 
}

any help please!


Solution

  • if you are binging the user through nested component and if relation is one-many through user and appointment (understanding that an user have one or many appointments) I think must be something like:

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