1

i'm trying to send a patch request from my Vuetify data-table to Laravel & then to a mySQL DB.

Here's various code pieces from my controller.php, my api.php & the actual Vuetify file:

api.php:

Route::patch('machines/{id}', [
    'as'   => 'machines/{id}',
    'uses' => 'MachineController@update'
]);

MachineController.php

$machines = Machine::find($request->id)->update();

the actual axios patch req. in the .vue file:

Object.assign(this.machines[this.editedIndex], this.editedItem);

axios.patch("machines/" + this.editedItem.id, {
    editedItem: this.editedItem
})

In the Telescope payload section i'm getting the updated object, but i'm also getting a message:

"SQLSTATE[23000]: Integrity constraint violation: 1048 Column cannot be null.

For all the columns.

I have also tried this syntax for a patch method:

if (this.editedIndex > -1) {
    Object.assign(this.machines[this.editedIndex], this.editedItem);
    axios
        .patch("machines/" + this.editedItem.id)
        .then(res => {
            this.editedItem = Object.assign({}, this.editedItem);
        })
        .catch(err => {
            console.log(err);
        });
} else {
    this.machines.push(this.editedItem);
}
this.close();

And i tried setting up the controller like this:

$machines = Machine::find($request->id);
$machines->machine_number = $request->input('machine_number');
$machines->machine_name = $request->input('machine_name');
$machines->machine_company = $request->input('machine_company');
$machines->machine_division = $request->input('machine_division');
$machines->machine_center = $request->input('machine_center');
$machines->machine_speed = $request->input('machine_speed');
$machines->save();

But I'm still getting the same error. Can someone help me out, or at least point me in the right direction? Thanks!

Rwd
  • 34,180
  • 6
  • 64
  • 78
Pok3r Princ3
  • 147
  • 2
  • 13
  • Have you `dd($request->all())` to see if anything is being passed through? – thisiskelvin May 13 '19 at 14:22
  • @thisiskelvin I just did, and nothing happened. Nothing showed anywhere, nothing changed ;/. Not quite: when i added ```dd($request->all());``` to the controller, Telescope stopped registering the patch request. – Pok3r Princ3 May 13 '19 at 14:27
  • Are you against using `post` instead of `patch`? Not that this is the solution. – thisiskelvin May 13 '19 at 14:30
  • @thisiskelvin From my understanding, in order to update selected fields from a table, you should use patch req. I might be wrong tho. – Pok3r Princ3 May 13 '19 at 14:31
  • Not necessarily, passing an `id` visually signifies an `update`. Without, it should be assumed you are trying to `store` a new record. – thisiskelvin May 13 '19 at 14:32
  • Okay, i'll give it a shot by using ```post```, but what bothers me is that apparently the data is somehow lost between the vue & controller (i'm logging it once i send the ```patch``` req. and it's there, it's in the Telescope payload, but it doesn't reach the DB. – Pok3r Princ3 May 13 '19 at 14:35
  • Thats the part Im concerned with. This could be an axios issue. Best way to test is to change from `patch` to `post` and see if all works as intended. – thisiskelvin May 13 '19 at 14:37
  • @thisiskelvin I changed the methods from ```patch``` to ```post``` both in the axios req. & the controller, but it behaves the same as before. I'm relatively new to Laravel & mySQL connecting to Vue, is there any more information i can provide that will help in solving the issue ? – Pok3r Princ3 May 13 '19 at 14:42
  • Are you using the code with contains the `this.close()` statement? If so, you're `patch` method doesnt have data passed in as the second argument. – thisiskelvin May 13 '19 at 14:44
  • @thisiskelvin This is the code in the `this.close()` method: ` close() { this.dialog = false; setTimeout(() => { this.editedItem = this.editedItem; this.editedIndex = -1; }, 300); } ` However, i've commented it out and i'm just calling ` this.dialog = false; ` to close the dialog window when i save – Pok3r Princ3 May 13 '19 at 14:45
  • @thisiskelvin I removed both the `this.close()` method & the `this.dialog = false;`, but it didn't help. ;/ – Pok3r Princ3 May 13 '19 at 14:48
  • More so talking about adding the data to the `patch` method, at the moment its only `.patch("machines/" + this.editedItem.id)`, but should be `.patch("machines/" + this.editedItem.id, { ... })` – thisiskelvin May 13 '19 at 14:52
  • @thisiskelvin Here's how my `patch` method looks: ` axios .patch("machines/" + this.editedItem.id, { editedItem: this.editedItem }) ` I believe i'm passing the data after the id ? My idea is that the editedItem is an object, and this.editedItem is an object as well, so that's why i'm passing it like this. Is it wrong ? – Pok3r Princ3 May 13 '19 at 14:56
  • @thisiskelvin I have structured my `patch` request like this, same result: `axios .patch("machines/" + this.editedItem.id, { id: this.editedItem.id, machine_number: this.editedItem.machine_number, machine_name: this.editedItem.machine_name, machine_company: this.editedItem.machine_company, machine_division: this.editedItem.machine_division, machine_center: this.editedItem.machine_center, machine_speed: this.editedItem.machine_speed }) ` *Also tried it with `post`. – Pok3r Princ3 May 13 '19 at 15:01

1 Answers1

1

I have solved my issue: I was passing empty object with my axios.patch() request because i've set it up wrong. I've changed the object's structure to key:value pairs and voila, it worked!

Pok3r Princ3
  • 147
  • 2
  • 13