duanmei2459 2016-02-21 12:41
浏览 81
已采纳

无法查询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

  • 写回答

2条回答 默认 最新

  • doujianchao7446 2016-02-22 00:47
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀