如何使用yajra过滤变形关系?

时间:2019-06-19 04:37:59

标签: laravel laravel-5.2 yajra-datatable

我在laravel项目中使用yajra数据表,并且创建了一个类,该类扩展了雄辩的数据表,该表用于过滤关系数据上的关系列。但我无法过滤有关变体关系的数据。

        $table = '';
        $deletedAt = false;
        $lastQuery = $this->query;
        foreach (explode('.', $relation) as $eachRelation) {
            $model = $lastQuery->getRelation($eachRelation);
            switch (true) {
                case $model instanceof BelongsToMany:
                    return  $relation . '.' . $relationColumn;
                    // belongs to many relationship does not work properly.
                    /*$pivot = $model->getTable();
                    $pivotPK = $model->getExistenceCompareKey();
                    $pivotFK = $model->getQualifiedParentKeyName();
                    $this->performJoin($pivot, $pivotPK, $pivotFK);

                    $related = $model->getRelated();
                    $table = $related->getTable();
                    // $tablePK = $related->getForeignKey() changed to $model->getRelatedPivotKeyName()
                    $tablePK = $model->getRelatedPivotKeyName();
                    $foreign = $pivot . '.' . $tablePK;
                    $other = $related->getQualifiedKeyName();

                    // removed conflict code when retrieving belongs to many relations data
                    $lastQuery->addSelect($table . '.' . $relationColumn . ' as ' . $table . '_'
                        . $relationColumn );
                    $this->performJoin($table, $foreign, $other);*/

                    //break;

                case $model instanceof HasOneOrMany:
                    $table = $model->getRelated()->getTable();
                    $foreign = $model->getQualifiedForeignKeyName();
                    $other = $model->getQualifiedParentKeyName();
                    $deletedAt = $this->checkSoftDeletesOnModel($model->getRelated());
                    break;

                case $model instanceof BelongsTo:
                    $table = $model->getRelated()->getTable();
                    $foreign = $model->getQualifiedForeignKeyName();
                    $other = $model->getQualifiedOwnerKeyName();
                    $deletedAt = $this->checkSoftDeletesOnModel($model->getRelated());
                    break;

                default:
                    throw new Exception('Relation ' . get_class($model) . ' is not yet supported.');
            }
            $this->performJoin($table, $foreign, $other, $deletedAt);
            $lastQuery = $model->getQuery();
        }

        return $table . '.' . $relationColumn;
    }

我想使用变形关系来检索数据,在我的情况下,项目是主题或分支。所以我想在回复中的商品名称上添加一个过滤器

 {
                "id": 27610,
               "items": [{
                            "item_id": 34245,
                            "expire_date": "2019-07-26 00:00:00",
                            "parent": null,
                            "item": {
                                "id": 3,
                                "name": "mechanical-engineering",
                                "short_description": "used subject - 21",
                                "long_description": "1",
                                "type": "live",
                                "slug": "test-data-1",
                                "is_active": 1,
                                "thumbnail_file": null,
                                "detail": {
                                    "id": 3,
                                    "start_date_time": "2019-08-23T06:29:35.000000Z",
                                    "end_date_time": "2019-08-23T06:29:35.000000Z",
                                    "actual_price": 182,
                                    "discounted_price": 110
                                }
                            },
                            "amount": 110,
                            "item_type": "live"
                        }
                    ],

            },

我已添加此表图像链接的数据库结构:-

[https://i.stack.imgur.com/PCGlf.png][1]

0 个答案:

没有答案