为了账号安全,请及时绑定邮箱和手机立即绑定

具有关系的模型的自定义字段/列属性

具有关系的模型的自定义字段/列属性

PHP
慕娘9325324 2021-06-29 17:24:32
我试图弄清楚在设置具有关系的字段时如何创建自定义属性。所以,我有学生谁是有一个的hasMany有关系促销活动。现在我的问题是我想要的属性在另一个模型/表中。例如,我需要腰带颜色,但腰带颜色不存储在促销中.. 促销表只包含Belt_id。我需要从腰带表中提取该腰带的颜色。我已经设置了所有关系,但我不知道如何操作该属性。我希望这是有道理的。$this->crud->addField([    'label' => "Promotions",    'type' => 'select2_multiple',    'name' => 'promotion',    'entity' => 'promotion',     'attribute' => 'name', // <- how can I make a custom query for this?    'pivot' => true, ]);
查看完整描述

1 回答

?
青春有我

TA贡献1784条经验 获得超8个赞

看起来我找到了一个解决方案并修复了一个小错误..至少它看起来像是一个错误,因为我找不到此功能的文档..我通过检查代码找到了它。无论如何,很酷的事情是我们可以用“。”链接关系,因此列设置将如下所示:


$this->crud->addColumn([

   'label' => "Rank", // Table column heading

   'type' => "select",

   'name' => 'promotion_id', // the column that contains the ID of that connected entity;

   'entity' => 'promotion.belt', // <- here I'm chaining the model relationships

   'attribute' => 'color', // the belt's attribute

   'model' => "App\Models\Promotion" // foreign key model

]);

从理论上讲,一切都应该正常工作,但是位于以下位置的方法中有一个小错误vendore\backpack\crud\src\CrudPanel.php:


private function getRelationModelInstances($model, $relationString)

{

    $relationArray = explode('.', $relationString);

    $firstRelationName = array_first($relationArray);


    // this is the line with the bug

    //$relation = $model->{$firstRelationName};


    // Fix the bug above

    if($model instanceof Collection) {

        $relation = $model->{$firstRelationName};

    } else {

        $relation = $model[$firstRelationName];

    }


    $results = [];

    if (! empty($relation)) {

        if ($relation instanceof Collection) {

            $currentResults = $relation->toArray();

        } else {

            $currentResults[] = $relation;

        }


        array_shift($relationArray);


        if (! empty($relationArray)) {

            foreach ($currentResults as $currentResult) {

                $results = array_merge($results, $this->getRelationModelInstances($currentResult, implode('.', $relationArray)));

            }

        } else {

            $results = $currentResults;

        }

    }



    return $results;

}

基本上,这个递归函数在调用自身时将$model参数作为arraynot object..传递,因此当它试图$firstRelationName从$model类似中获取时,这$model->{$firstRelationName}将导致错误。


同时我发现了一个更好的解决方案。为了实现相同的目标,我使用了accessors.


在我的promotion模型中,我有:


    /*

    |--------------------------------------------------------------------------

    | RELATIONS

    |--------------------------------------------------------------------------

    */

     public function belt()

    {

        return $this->belongsTo('App\Models\Belt');

    }


    /*

    |--------------------------------------------------------------------------

    | ACCESORS

    |--------------------------------------------------------------------------

    */

    public function getPromotionSummaryAttribute()

    {

        return $this->belt()->get()[0]->color . ', ' . $this->stripe . ' stripe';

    }

因为students我有:


    /*

    |--------------------------------------------------------------------------

    | RELATIONS

    |--------------------------------------------------------------------------

    */

    public function promotion()

    {

        return $this->hasMany('App\Models\Promotion');

    }


    /*

    |--------------------------------------------------------------------------

    | ACCESORS

    |--------------------------------------------------------------------------

    */


    public function GetLastPromotionAttribute()

    {

        return $this->promotion()->get()->last()->promotion_summary;

    }

列设置将像这样简单:


   $this->crud->addColumn([

        'name' => 'last_promotion', 

        'type' => 'text', 

        'label' => 'Rank'

   ]);

我希望这会帮助那里的人:)


查看完整回答
反对 回复 2021-07-09
  • 1 回答
  • 0 关注
  • 116 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号