十月 Cms。如何给模型添加属性?
OctoberCms. How to add attribute to Model?
我需要按父模型 ID 显示子模型列表:
如果我在模型中使用它就可以工作:
public function getIdAttribute($id)
{
$sales = Sale::where('category_id',$id)->get();
foreach ($sales as $sale) {
$clean_fields[$sale->attributes['id']]['name'] = $sale->attributes['name'];
$clean_fields[$sale->attributes['id']]['price'] = $sale->attributes['price'];
}
return $clean_fields;
}
在模板中显示列表:
{% for service in servicesList %}
<h1>{{service.name}}</h1>
<ul class="children_list">
{% for children in service.id %}
<li>{{children.name}}</li>
{% endfor %}
<ul>
{% endfor %}
我修改属性id到数组中。它在模板中工作,但在后端我有错误,因为 id 没有传递给列表控制器。如何在组件模板中获取子模型?
为您的模型添加适当的关系
public function children()
{
return $this->hasMany(Sale::class,'category_id','id');
}
然后你 children 随心所欲
$model = Model::with('children')->find('id');
@foreach($model->children as $child)
@endforeach
我假设您有 2 个模型 Service
(父)和 Sale
(子)
I am also assuming from template for loop
that, it is hasMany
relation (where service
has many records of sale
)
其中 Sale
有 category_id
,这是父 id
。
所以在父模型中你可以定义关系
class Service extends Model
{
public $hasMany = [
'sales' => ['Acme\Blog\Models\Sale', 'key' => 'category_id']
// here you need to add proper namespace for your model
];
}
现在当您需要获取 Sale
的相关记录时,您可以调用此关系别名 sales
.
我们假设我们正在从组件传递 servicesList
。
public function onRun()
{
$this->page['servicesList'] = Service::all();
}
现在在页面内你可以写这样的东西,因为 servicesList
将对页面可用(它的模型集合 Service
)
{% for service in servicesList %}
<h1>{{service.name}}</h1>
<ul class="children_list">
{% for children in service.sales %}
<!-- its relation name 'service.sales' which we defined in model -->
<li>{{children.name}}</li>
{% endfor %}
<ul>
{% endfor %}
如果您还有任何疑问,请在评论中告诉我。
我需要按父模型 ID 显示子模型列表: 如果我在模型中使用它就可以工作:
public function getIdAttribute($id)
{
$sales = Sale::where('category_id',$id)->get();
foreach ($sales as $sale) {
$clean_fields[$sale->attributes['id']]['name'] = $sale->attributes['name'];
$clean_fields[$sale->attributes['id']]['price'] = $sale->attributes['price'];
}
return $clean_fields;
}
在模板中显示列表:
{% for service in servicesList %}
<h1>{{service.name}}</h1>
<ul class="children_list">
{% for children in service.id %}
<li>{{children.name}}</li>
{% endfor %}
<ul>
{% endfor %}
我修改属性id到数组中。它在模板中工作,但在后端我有错误,因为 id 没有传递给列表控制器。如何在组件模板中获取子模型?
为您的模型添加适当的关系
public function children()
{
return $this->hasMany(Sale::class,'category_id','id');
}
然后你 children 随心所欲
$model = Model::with('children')->find('id');
@foreach($model->children as $child)
@endforeach
我假设您有 2 个模型 Service
(父)和 Sale
(子)
I am also assuming from
template for loop
that, it ishasMany
relation (whereservice
has many records ofsale
)
其中 Sale
有 category_id
,这是父 id
。
所以在父模型中你可以定义关系
class Service extends Model
{
public $hasMany = [
'sales' => ['Acme\Blog\Models\Sale', 'key' => 'category_id']
// here you need to add proper namespace for your model
];
}
现在当您需要获取 Sale
的相关记录时,您可以调用此关系别名 sales
.
我们假设我们正在从组件传递 servicesList
。
public function onRun()
{
$this->page['servicesList'] = Service::all();
}
现在在页面内你可以写这样的东西,因为 servicesList
将对页面可用(它的模型集合 Service
)
{% for service in servicesList %}
<h1>{{service.name}}</h1>
<ul class="children_list">
{% for children in service.sales %}
<!-- its relation name 'service.sales' which we defined in model -->
<li>{{children.name}}</li>
{% endfor %}
<ul>
{% endfor %}
如果您还有任何疑问,请在评论中告诉我。