【问题标题】:How to delete multiple rows using checkbox in Laravel 5.5 + Vue.js + Axios如何在 Laravel 5.5 + Vue.js + Axios 中使用复选框删除多行
【发布时间】:2018-10-01 00:19:10
【问题描述】:

我正在尝试使用复选框来删除多个项目。我正在使用 Laravel 5.5、Vue.js 和 Bulma 的组件 - Buefy + Axios。

我的 Vue.js 组件:

<template>
<section>
    <button class="button field is-small is-danger" @click="checkedRows = []"
        :disabled="!checkedRows.length">
        <b-icon icon="close"></b-icon>
        <span>Clear checked</span>
    </button>
    <button class="is-small button is-danger" @click.prevent="onDelete" title="Delete checked" :disabled="!checkedRows.length"><i class="icon-trash"></i></button>

    <b-field grouped group-multiline>                      
        <b-select v-model="perPage" :disabled="!isPaginated" size="is-small">
            <option value="5">5 per page</option>
            <option value="10">10 per page</option>
            <option value="15">15 per page</option>
            <option value="20">20 per page</option>
        </b-select>
        <div class="control">
            <button class="button is-small" @click="currentPage = 2" :disabled="!isPaginated">Set page to 2</button>
        </div>
        <div class="control is-flex">
            <b-switch v-model="isPaginated" size="is-small">Paginated</b-switch>
        </div>
        <div class="control is-flex">
            <b-switch v-model="isPaginationSimple" :disabled="!isPaginated" size="is-small">Simple pagination</b-switch>
        </div>
    </b-field>

    <b-table
        :data="enquiries"
        :paginated="isPaginated"
        :per-page="perPage"
        :current-page.sync="currentPage"
        :pagination-simple="isPaginationSimple"
        :default-sort-direction="defaultSortDirection"
        default-sort="created_at"
        :checked-rows.sync="checkedRows"
        :is-row-checkable="(row) => row.id !== 3"
        checkable>

        <template slot-scope="props">
            <b-table-column field="id" label="ID" width="40" sortable numeric>
                <small>{{ props.row.id }}</small>
            </b-table-column>

            <b-table-column field="date" label="Registration date" sortable centered>
                <span class="tag is-success">
                    {{ new Date(props.row.created_at).toLocaleDateString() }}
                </span>
            </b-table-column>

            <b-table-column field="company" label="Company" sortable>
                <small>{{ props.row.company }}</small>
            </b-table-column>

            <b-table-column field="first_name" label="First Name" sortable>
                <small>{{ props.row.first_name }}</small>
            </b-table-column>

            <b-table-column field="last_name" label="Last Name" sortable>
                <small>{{ props.row.last_name }}</small>
            </b-table-column>   

            <b-table-column field="email" label="Email" sortable>
                <small>{{ props.row.email }}</small>
            </b-table-column> 

            <b-table-column field="phone" label="Phone" sortable>
                <small>{{ props.row.phone }}</small>
            </b-table-column> 

        </template>
    </b-table>
</section>

<script>

export default {
    data() {
        return {                      
            enquiries: [],
            errors: [],
            isPaginated: true,
            isPaginationSimple: false,
            defaultSortDirection: 'asc',
            currentPage: 1,
            perPage: 5,
            checkedRows: []

        }
    },       

    created() {
        axios.get('/manage/demo-enquiries')
             .then(response => {
                this.enquiries = response.data  
             })
             .catch(e => {
                this.errors.push(e)
             })
    }, 

    methods: {
        onDelete() {
            axios.delete('/manage/demo-enquiries', {params: {'id': 
                this.checkedRows}})
                .then((response) => {
                    console.log(response)
                }, (error) => {
                    // error callback
            })
        }
    }       
}

路线:

Route::delete('manage/demo-enquiries/', 'DemorequestController@destroy');

和控制器:

public function destroy(Request $request)
{
    try 
    {
        Demorequest::whereIn('id', $request->id)->delete(); // $request->id MUST be an array
        return response()->json('Enquiry deleted');
    }

    catch (Exception $e) {
        return response()->json($e->getMessage(), 500);
    }
}

当我选择几行并单击删除时,浏览器控制台显示“{data: "Enquiry deleted", status: 200, statusText: "OK", headers: {…}, config: {…}, …}"但不会从数据库中删除任何内容。

任何人都可以帮助找到使用复选框的多删除解决方案吗?

【问题讨论】:

  • 你检查过请求中是否正确传递了数组吗?
  • hmmm....我不这么认为....我的请求 URL 是 /demo-enquiries?id[]=%7B%22id%22:40,%22first_name%22:% 22Anton%22,%22last_name%22:%22Sirik%22,%22email%22:%22anton.sirik@i-nexus.com%22,%22job_title%22:%22Marketing+Executive%22,%22company%22:% 22i- 以此类推
  • $request 对象的输出是什么?
  • 请求对象是上面url中的null,这意味着问题是你没有传递任何id,你能检查一下这个数组是否在vue devtools中有任何数据
  • @Jackowski,我相信这是应要求完成的: id[]: {"id":39,"first_name":"Anton","last_name":"Sirik","email ":"anton.sirik@i-nexus.com","job_title":"Marketing","company":"i-nexus","phone":"+447507633865","consent":"1"," created_at":"2018-04-18 13:35:41","updated_at":"2018-04-18 13:35:41"} id[]: {"id":40,"first_name":"Anton ","last_name":"Sirik","email":"anton.sirik@i-nexus.com","job_title":"营销主管","company":"i-nexus","电话":" +447507633865","同意":"1","created_at":"2018-04-18 13:37:29","updated_at":"2018-04-18 13:37:29"}

标签: ajax laravel vue.js axios


【解决方案1】:

您只需要使用array_column()从对象数组中提取id

public function destroy(Request $request)
{
    try 
    {
        $ids = array_column($request->id, "id");
        Demorequest::whereIn('id', $ids)->delete(); 
        return response()->json('Enquiry deleted');
    }

    catch (Exception $e) {
        return response()->json($e->getMessage(), 500);
    }
}

另外,另一种不接触 php 代码的方法是只从复选框中获取 id。

this.checkedRows.map(a=&gt;a.id)

【讨论】:

  • 我得到 405 Method Not Allowed :(
  • @AntonSirik 将 Route::delete('manage/demo-enquiries/', 'DemorequestController@destroy') 更改为 Route::get('manage/demo-enquiries/', 'DemorequestController@destroy')
  • 通过检查行数组而不是 ids
  • 仍然得到 405 Method Not Allowed
  • DELETE /manage/demo-enquiry?id[]=40&id[]=39&id[]=38&id[]=37&id[]=36 405(方法不允许)
猜你喜欢
  • 1970-01-01
  • 2019-02-24
  • 2018-04-20
  • 1970-01-01
  • 2015-02-06
  • 1970-01-01
  • 1970-01-01
  • 2018-06-27
  • 2020-03-08
相关资源
最近更新 更多