Japs
Japs

Reputation: 1052

I'm having trouble with "AXIOS PATCH" method

I am trying to update my database record using "axios patch" method.

This is my code:

editClient(client) {
  let data = new FormData();
  data.append("name", this.client.name);
  data.append("email", this.client.email);
  data.append("phone", this.client.phone);
  data.append("_method", "PATCH");
  axios
    .post(`/api/clients/${client.id}`, data)
    .then(res => {
      resolve(res.data.client);
    })
    .catch(err => console.log(err.response.data));
},

I tried this code:

axios
    .patch(`/api/clients/${client.id}`, {
      name: this.client.name,
      phone: this.client.phone,
      email: this.client.email
    })
    .then(res => {
      resolve(res.data.client);
    })
    .catch(err => console.log(err.response.data));

But it also dosn't work.

The error i get is

POST http://localhost:8000/api/clients/27 500 (Internal Server Error)

{message: "SQLSTATE[42S22]: Column not found: 1054 Unknown co…4 02:12:57"` = updated_at:"2019-05-24 02:12:57"})", exception: "Illuminate\Database\QueryException", file: "C:\xampp\htdocs\prs3\vendor\laravel\framework\src\Illuminate\Database\Connection.php", line: 664, trace: Array(60)}
exception: "Illuminate\Database\QueryException"
file: "C:\xampp\htdocs\prs3\vendor\laravel\framework\src\Illuminate\Database\Connection.php"
line: 664
message: "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name:"2011"' in 'where clause'

And when I tried this code:

axios
    .patch(`/api/clients/${client.id}`, {
      data: this.client
    })
    .then(res => {
      resolve(res.data.client);
    })
    .catch(err => console.log(err.response.data));

The error i get is

app.js:285 PATCH http://localhost:8000/api/clients/27 422 (Unprocessable Entity)

{message: "The given data was invalid.", errors: {…}}
errors:
email: ["The email field is required."]
name: ["The name field is required."]
phone: ["The phone field is required."]
__proto__: Object
message: "The given data was invalid."

Im new in axios and vue. Im trying to learn on how to build and CRUD api using axios.

I tried to find other ways and i can't find any.

This is my controller:

public function update(Client $client) {
        $val = $client ? ','.$client : '';
        $client->update($this->_val($val));
        return back();
    }

    public function _val($val) {
        return request()->validate([
            'name' => ['required', 'min:2', 'unique:clients,name'.$val],
            'email' => ['required', 'email', 'unique:clients,email'.$val],
            'phone' => ['required', 'alpha_num'],
        ]);
    }

Upvotes: 3

Views: 20221

Answers (1)

Karan Sadana
Karan Sadana

Reputation: 1373

Your axios post code is fine. It's working fine. The error you get in the post field is larvel error column not found.

I guess you made a typo in some column name while saving data or you entered unknown column by mistake which is not in the table.

Please provide laravel side code, so we can see where you facing an error. By the way from your question => Your axios.post code is correct

Upvotes: 1

Related Questions