I try to using Livewire Binding Directly To Model Properties, and i get this error, Anyone help please ? Thank in advance.
Typed property App\Http\Livewire\V2\Settings\Locations::$country must not be accessed before initialization
class Locations extends Component{
public Country $country;
use WithPagination;
protected $rules = [
'country.name' => 'required|min:100',
'country.note' => 'required|min:100',
];
public function SaveCountry(){
$this->validate();
$this->country->save();
$this->reset();
session()->flash('info','The country have been added successful.');
}
public function render(){
return view('livewire.v2.settings.locations',[
'countries' => Country::orderBy('order_num','desc')->paginate(10),
]);
}
}
When you define the type of a variable in PHP, you need to provide it a default value.
If your component is creating a Country
, you can set it to an empty Country
in the mount
function:
public function mount()
{
$this->country = new Country();
}
Alternatively set the public Country $country
to be an existing Country
.