How to pass a Model instance to Livewire in Laravel

Categorized as Livewire, Laravel

If you want to pass a Model to your Laravel Livewire component, you can do it via public property instead of adding the code to the mount() method.

Take a look at this code excerpt:

// 🤔 instead of this 

use App\Models\User;

class UserDetails extends Component
{
  public $user;
  
  public function mount(User $user)
  {
      $this->user = $user;
  }

  ...

}
// ✅  use this 

use App\Models\User;

class UserDetails extends Component
{
  public User $user;
  ...

}

By @sky_0xs

Leave a reply

Your email address will not be published. Required fields are marked *