Level 70
@armani Like this?
public function rules()
{
return [
'unit_id' => [
'required',
Rule::unique('units')->where(function ($query) {
return $query->where('name', $this->unit_id);
}),
],
];
}
public function prepareForValidation()
{
$this->merge(
[
'unit_id' => is_numeric($this->unit_id) ? $this->unit_id :
Unit::firstOrCreate(['name' => $this->unit_id])->id,
]
);
}
1 like