Laravel数据表过滤给我不同的结果

时间:2018-10-18 09:03:10

标签: datatables laravel-5.5

在终端上运行此示例SQL查询时

        SELECT users.name as cutomersName, gifts.shipment_id AS shipmentID, fulfillments.id AS fulfillmentId, subscriptions.id AS subscriptionId FROM gifts
        INNER JOIN shipments on shipments.id = gifts.shipment_id
        INNER JOIN fulfillments ON fulfillments.id = shipments.fulfillment_id
        INNER JOIN subscriptions ON subscriptions.id = fulfillments.subscription_id
        INNER JOIN users ON users.id = subscriptions.user_id
        WHERE gifts.shipment_id = 11

我得到了这个结果 enter image description here

仅仅是示例,打算做的是过滤,这意味着此WHERE gifts.shipment_id = 11条件应该是动态的。就像这张图片所示。

enter image description here

当我选择All gifts subscriptions时,它应该返回Graham Barry行,并且可能会很多。

到目前为止,这是我的数据表类。

<?php

namespace <path_to>\Subscriptions\DataTables;

use Carbon\Carbon;
use <path_to>\DataTables\BaseDataTable;
use <path_to>\Modules\Subscriptions\Facades\Products;
use <path_to>\Modules\Subscriptions\Models\Subscription;
use <path_to>\Subscriptions\Transformers\SubscriptionTransformer;
use Yajra\DataTables\EloquentDataTable;

class SubscriptionsDataTable extends BaseDataTable
{
    /**
     * @var
     */
    private $search_val;
    private $_filters;

    public function __construct()
    {
        $this->search_val = strip_left_zero($this->request()->search['value']);
        parse_str($this->request->filters, $this->_filters);
    }

    /**
     * Get query source of dataTable.
     * @param Subscription $model
     * @return \Illuminate\Database\Eloquent\Builder|static
     */
    public function query(Subscription $model)
    {
        if ($this->_filters['gifting'] == '1') {
            $model = $model->join('shipments', 'shipments.id', '=', 'gifts.shipment_id')
                ->join('fulfillments', 'fulfillments.id', '=', 'shipments.fulfillment_id')
                ->join('subscriptions', 'subscriptions.id', '=', 'fulfillments.subscription_id')
                ->join('users', 'users.id', '=', 'subscriptions.user_id');
            /*
            # this is just example of table fields but not the actual
            SELECT users.name as cutomersName, gifts.shipment_id AS shipmentID, fulfillments.id AS fulfillmentId, subscriptions.id AS subscriptionId FROM gifts
            INNER JOIN shipments on shipments.id = gifts.shipment_id
            INNER JOIN fulfillments ON fulfillments.id = shipments.fulfillment_id
            INNER JOIN subscriptions ON subscriptions.id = fulfillments.subscription_id
            INNER JOIN users ON users.id = subscriptions.user_id
            WHERE gifts.shipment_id = 11
             */
        }

        return $model->with('plan')->with('user')->newQuery();
    }

    /**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
            'id'           => ['title' => 'ID','visible'  => false],
            'user_id'      => ['title' => 'Customer', 'name' => 'user.name'],
            'product_id'   => ['title' => 'Product', 'searchable' => false],
            'revenue'      => ['title' => 'Revenue', 'searchable' => false],
            'sub_id'       => ['title' => 'Sub ID', 'searchable' => false],
            'created_at'   => ['title' => 'Sign-up Date'],
            'renewal_date' => ['title' => 'Renewal Date', 'searchable'    => false],
            'plan_id'      => ['title' => 'Term', 'searchable' => false],
            'active'       => ['title' => 'Status', 'searchable' => false],
            'imported'     => ['title' => 'Is Imported', 'name'=> 'user.fk_cratejoy_customers_id'],
        ];
    }
}

我在方法query()上执行上述sql查询。

但是这段代码给了我不同的结果。

您能给我建议或解决我的问题吗?

这是laravel,我正在从这里https://github.com/yajra/laravel-datatables使用

0 个答案:

没有答案