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

我试图弄清楚在设置具有关系的字段时如何创建自定义属性。


所以,我有学生谁是有一个的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, 

]);


慕娘9325324
浏览 117回答 1
1回答

青春有我

看起来我找到了一个解决方案并修复了一个小错误..至少它看起来像是一个错误,因为我找不到此功能的文档..我通过检查代码找到了它。无论如何,很酷的事情是我们可以用“。”链接关系,因此列设置将如下所示:$this->crud->addColumn([&nbsp; &nbsp;'label' => "Rank", // Table column heading&nbsp; &nbsp;'type' => "select",&nbsp; &nbsp;'name' => 'promotion_id', // the column that contains the ID of that connected entity;&nbsp; &nbsp;'entity' => 'promotion.belt', // <- here I'm chaining the model relationships&nbsp; &nbsp;'attribute' => 'color', // the belt's attribute&nbsp; &nbsp;'model' => "App\Models\Promotion" // foreign key model]);从理论上讲,一切都应该正常工作,但是位于以下位置的方法中有一个小错误vendore\backpack\crud\src\CrudPanel.php:private function getRelationModelInstances($model, $relationString){&nbsp; &nbsp; $relationArray = explode('.', $relationString);&nbsp; &nbsp; $firstRelationName = array_first($relationArray);&nbsp; &nbsp; // this is the line with the bug&nbsp; &nbsp; //$relation = $model->{$firstRelationName};&nbsp; &nbsp; // Fix the bug above&nbsp; &nbsp; if($model instanceof Collection) {&nbsp; &nbsp; &nbsp; &nbsp; $relation = $model->{$firstRelationName};&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $relation = $model[$firstRelationName];&nbsp; &nbsp; }&nbsp; &nbsp; $results = [];&nbsp; &nbsp; if (! empty($relation)) {&nbsp; &nbsp; &nbsp; &nbsp; if ($relation instanceof Collection) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $currentResults = $relation->toArray();&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $currentResults[] = $relation;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; array_shift($relationArray);&nbsp; &nbsp; &nbsp; &nbsp; if (! empty($relationArray)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($currentResults as $currentResult) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $results = array_merge($results, $this->getRelationModelInstances($currentResult, implode('.', $relationArray)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $results = $currentResults;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return $results;}基本上,这个递归函数在调用自身时将$model参数作为arraynot object..传递,因此当它试图$firstRelationName从$model类似中获取时,这$model->{$firstRelationName}将导致错误。同时我发现了一个更好的解决方案。为了实现相同的目标,我使用了accessors.在我的promotion模型中,我有:&nbsp; &nbsp; /*&nbsp; &nbsp; |--------------------------------------------------------------------------&nbsp; &nbsp; | RELATIONS&nbsp; &nbsp; |--------------------------------------------------------------------------&nbsp; &nbsp; */&nbsp; &nbsp; &nbsp;public function belt()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->belongsTo('App\Models\Belt');&nbsp; &nbsp; }&nbsp; &nbsp; /*&nbsp; &nbsp; |--------------------------------------------------------------------------&nbsp; &nbsp; | ACCESORS&nbsp; &nbsp; |--------------------------------------------------------------------------&nbsp; &nbsp; */&nbsp; &nbsp; public function getPromotionSummaryAttribute()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->belt()->get()[0]->color . ', ' . $this->stripe . ' stripe';&nbsp; &nbsp; }因为students我有:&nbsp; &nbsp; /*&nbsp; &nbsp; |--------------------------------------------------------------------------&nbsp; &nbsp; | RELATIONS&nbsp; &nbsp; |--------------------------------------------------------------------------&nbsp; &nbsp; */&nbsp; &nbsp; public function promotion()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->hasMany('App\Models\Promotion');&nbsp; &nbsp; }&nbsp; &nbsp; /*&nbsp; &nbsp; |--------------------------------------------------------------------------&nbsp; &nbsp; | ACCESORS&nbsp; &nbsp; |--------------------------------------------------------------------------&nbsp; &nbsp; */&nbsp; &nbsp; public function GetLastPromotionAttribute()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->promotion()->get()->last()->promotion_summary;&nbsp; &nbsp; }列设置将像这样简单:&nbsp; &nbsp;$this->crud->addColumn([&nbsp; &nbsp; &nbsp; &nbsp; 'name' => 'last_promotion',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 'type' => 'text',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 'label' => 'Rank'&nbsp; &nbsp;]);我希望这会帮助那里的人:)
打开App,查看更多内容
随时随地看视频慕课网APP