I try to take customer (name, address) based on phone number input. if customer is there then I want the name and address to auto-filled based on customer data we got. below are my code.
my form
<v-row>
<v-col cols="12">
<v-text-field :rules="contactRules" v-model="form.contact" label="Contact" type="number" @input="searchCustomerContact"></v-text-field>
</v-col>
<v-col cols="12">
<v-text-field :rules="nameRules" v-model="form.name" label="Name"></v-text-field>
</v-col>
<v-col cols="12">
<v-textarea rows="1" :rules="address1Rules" v-model="form.address_line_1" label="Address" auto-grow clearable clear-icon="cancel"></v-textarea>
</v-col>
</v-row>
data()
data() {
return {
valid: false,
form: {},
nameRules: [v => !!v || "Name is required"],
contactRules: [v => !!v || "Contact is required"],
address1Rules: [v => !!v || "Address is required"]
};
},
search Customer ()
searchCustomerContact(val) {
if (val.length == 10) {
this.$axios
.get("customers?contact=" + val)
.then(res => {
if (res.data != null) {
this.form.name = res.data.name;
this.form.address_line_1 = res.data.address_line_1;
} else {
this.form.name = null;
this.form.address_line_1 = null;
}
})
.catch(err => {
throw err;
});
}
}
My controller
public function getCustomerByContact()
{
$data = Order::where('contact', request()->contact)->first();
return response()->json($data, 200);
}
Route
Route::get('customers', 'OrderController@getCustomerByContact');
My problem is when data is found, it cannot auto-filled the name and address field. I'm new in vue. If contact is not there then the (name, contact) field is always empty so I think no need to do that part (maybe)
thanks in advance