Search code examples
phpjsonlaravel-5vue.jsvue-resource

Can't query JSON (Laravel + VueJS)


my problem exactly smiliar with this one cant't query json data in laravel 5.2

Already try to implement the right answer from it but still, no luck. I don't know why....

Previous, i found this Laravel 5.2 Codeception functional test issue with PUT / PATCH requests too, already try to use suggestion from him, but no luck too.

Here's my Laravel Controller

public function update(Request $request, $id)
{
    $phonebook = Phonebook::findOrFail($id);
    $phonebook->update($request->all());

    // even i try this
    // Phonebook::findOrFail($id)->update($request->all());
    // return Response::json() or return response()->json();
    // No luck
}

My function in vue script for update data

editContact: function(id)
    {
        this.edit = true
        var contactid = this.newContact.ID

        this.$http.patch('/api/contact/' + contactid, this.newContact, function (data) {
            console.log(data)
        })
    },

Change my vue script to be like the right answer from question above, same result. No effect.

And my button to do edit like this

<form action="#" @submit.prevent="addNewContact">

        <div class="form-group">
            <label for="contactName">Name : </label>
            <input type="text" v-model="newContact.CONTACTNAME" class="form-control" id="contactName">
        </div>

        <div class="form-group">
            <label for="phoneNumber">Phone number : </label>
            <input type="text" v-model="newContact.PHONENUMBER" class="form-control" id="phoneNumber">
        </div>

        <div class="form-group">
            <button class="btn btn-primary btn-sm" type="submit" v-if="!edit">Add new Contact</button>
            <button class="btn btn-primary btn-sm" type="submit" v-if="edit" @click="editContact(newContact.ID)">Edit Contact</button>
        </div>

    </form>

Note : My route file using resource or manual route always same

Route::resource('/api/contact/', 'PhonebookController');

or

patch('/api/contact/{id}', ['uses' => 'PhoneboookController@update']);

And then, there something strange. Detail request

(Maybe i am wrong) there no issue or error if we look the detail. But, if we change to response tab the result was empty No response!

After all that process, nothing happen with the data.

CONTACTNAME should be "Mizukiaaaaaaaa" like first screenshot instead of "Mizuki" enter image description here

Am I missing something?? Any advise?

Thanks


Solution

  • After browsing and ask so much people about this, finally found it! There's nothing wrong with the request or response. My mistakes are mutator update that i used and my model.

    Updated answer

    Reason answered here and then I just changed update function on controller. Here the result

    public function update(Phonebook $phonebook, Request $request, $id)
    {
        // You can add any fields that you won't updated, usually primary key
        $input = $request->except(['ID']);
    
        // Update query
        $saveToDatabase = $phonebook->where('ID', '=', $id)->update($input);
        return $saveToDatabase;
    }
    

    My previous answer updated all fields including the primary key, somehow it successful update data, but it leave error for sure (duplicate primary key). The query looks like UPDATE SET field = 'value' without condition.

    This case is for model that doesn't have any relation with other models (tables), or the model act as master.