【问题标题】:Metronic Datatable with Laravel and many-to-many relationship具有 Laravel 和多对多关系的 Metronic 数据表
【发布时间】:2020-06-02 15:54:44
【问题描述】:

我有以下问题:

我在客户和分支机构之间建立了多对多的关系。这是经过测试的并且正在工作。但是现在我不想使用这种关系在客户数据表中显示分支的名称。

我收到此错误:

“SQLSTATE [42S22]:找不到列:1054 '字段列表'中的未知列'branches.name'(SQL:选择客户。*,来自客户的分支.name,其中customers.deleted_at为空订单,id asc限制50 偏移量 0)"

我不确定如何在客户表旁边选择正确的表。

这是负责调用数据表类的控制器方法:

public function fetch(Request $request) {
    Notification::visit(CustomersRetrieved::class);
    return response()->json((new CustomerDatatable($request))->render());
}

这是数据表类:

<?php
/**
 * Created by PhpStorm.
 * User: NEWPC-1
 * Date: 4-1-2019
 * Time: 08:31
 */

namespace Modules\CRM\Datatables;

use App\Support\Datatables\Datatable;
use App\Support\Datatables\DatatableContract;
use Illuminate\Support\Facades\Log;
use Modules\CRM\Models\Customer;

class CustomerDatatable extends Datatable implements DatatableContract
{
    /**
     * @return mixed|string
     */
    public function model()
    {
        return Customer::class;
    }

    /**
     * @return array|mixed
     */
    public function orders()
    {
        return [
            'code' => 'code',
            'name' => 'name',
            'branch' => 'branch',
            'email' => 'email',
            'phone_number' => 'phone_number',
            'postal_code' => 'postal_code',
            'city' => 'city',
            'address' => 'address',
            'address_number' => 'address_number',
        ];
    }

    /**
     * @return mixed
     */
    protected function newQuery()
    {
        $query = parent::newQuery()->with('branches')->select(['customers.*','branches.name']);
        Log::info(json_encode($query));
        if(isset($this->input['query']) && strlen($q = $this->input['query']) > 0) {
            $query->search($q);
        }
        return $query;
    }

    /**
     * @return mixed
     */
    protected function getQueryCount()
    {
        $query = parent::newQuery();

        if(isset($this->input['query']) && strlen($q = $this->input['query']) > 0) {
            $query->search($q);
        }
        return $query->count();
    }

    public function exportable()
    {
        return [
            ['key' => 'name'],
            ['key' => 'branch'],
            ['key' => 'address'],
            ['key' => 'address_number'],
            ['key' => 'postal_code'],
            ['key' => 'city'],
        ];
    }
}

这是对应的javascript:

customer_datatable = customer_element.CustomDataTable({
        rows: {
            beforeTemplate: function (row, data, index) {
                row.on('click', function (e) {
                    if ($(e.target).hasClass('kt-badge--info') || $(e.target).hasClass('la') || $(e.target).hasClass('fa') || $(e.target).hasClass('btn') || $(e.target).hasClass('kt-datatable__cell--center')) {
                    } else {
                        window.open(customer_element.data('route-url') + '/crm/customers/' + data.id, '_blank');
                    }
                });
            },
        },
        detail: {
            title: customer_element.data('text-load_contacts'),
            content: contactsInit,
        },
        columns: [
            {
                field: 'id',
                title: '',
                sortable: false,
                width: 20,
                textAlign: 'center',
            },
            {
                field: 'code',
                width: 75,
                title: customer_element.data('column-code'),
                template: function (row) {
                    return row.code;
                }
            },
            {
                field: 'name',
                title: customer_element.data('column-name'),
                width: 300,
                template: function (row) {
                    return '<a href="' + customer_element.data('route-show').replace('__id__', row.id) + '">' + row.name + '</a>';
                }
            },
    //this is the part causing/part of the problem
            {
                field: 'branch',
                title: customer_element.data('column-branch'),
                width: 300,
                template: function (row) {
                    console.log(row);
                }
            },
            {
                field: 'email',
                title: customer_element.data('column-email'),
                width: 250,
                template: function (row) {
                    if (row.email === '') {
                        return '';
                    }
                    var e = row.email.split(','), r = new Array();

                    $.each(e, function (i, x) {
                        r.push('<a href="mailto:' + $.trim(x) + '" class="kt-badge kt-badge--info  kt-badge--inline kt-badge--pill">' + $.trim(x) + '</a>');
                    });
                    return '<span style="line-height:2rem !important;"  title="E-mail this customer">' + r.join('&nbsp;&nbsp;&nbsp;') + '</span>';
                }
            },
            {
                field: 'phone_number',
                title: customer_element.data('column-phone_number'),
                width: 100,
                template: function (row) {
                    if (row.phone_number.length === 0) {
                        return '';
                    }
                    let number = parsePhoneNumber(row.phone_number);

                    return '<a href="tel:' + phoneUtil.format(number, PNF.E164) + '" class="kt-badge kt-badge--brand  kt-badge--inline kt-badge--pill">' + phoneUtil.format(number, PNF.NATIONAL) + '</a>';
                }
            },
            {
                field: 'postal_code',
                title: customer_element.data('column-postal_code'),
                width: 100,
                template: function (row) {
                    return row.postal_code;
                }
            },
            {
                field: 'city',
                title: customer_element.data('column-city'),
                width: 100,
                template: function (row) {
                    return row.city;
                }
            },
        ],
    });

感谢 Ali Alghozali 和这篇文章,我终于解决了这个问题: MySQL join many to many single row

protected function newQuery()
    {
        $query = parent::newQuery()
                ->leftJoin('branch_customers', 'customers.id', '=', 'branch_customers.customer_id')
                ->leftJoin('branches', 'branches.id', '=', 'branch_customers.branch_id')
                ->select(['customers.*','branches.name as branch']);
        Log::info(json_encode($query));
        if(isset($this->input['query']) && strlen($q = $this->input['query']) > 0) {
            $query->search($q);
        }
        return $query;
    }

【问题讨论】:

    标签: laravel datatables metronic


    【解决方案1】:

    您无法选择with 表中的列。您需要改用joinleftJoin

    【讨论】:

    • 很高兴为您提供帮助?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多