【发布时间】:2015-01-07 17:19:38
【问题描述】:
鉴于以下 10 月 CMS 插件模型:
Schema::create('iaff106_cellphone_cellphones', function($table){
$table->engine = 'InnoDB';
$table->increments('id');
$table->integer('user_id')->unsigned()->nullable()->index();
$table->string('label');
$table->string('phone')->nullable()->index();
$table->integer('provider_id')->nullable();
$table->boolean('is_txtable')->default(true);
$table->boolean('is_published')->default(false);
$table->timestamps();
});
Schema::create('iaff106_cellphone_providers', function($table){
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('name');
$table->string('slug')->index();
$table->string('mail_domain')->nullable();
$table->timestamps();
});
提供者的关系:
public $belongsToMany = ['IAFF106\CellPhone\Models\Cellphone', 'foreignKey' => 'provider_id'];
手机关系:
public $hasOne = ['IAFF106\CellPhone\Models\Provider'];
我想选择一个手机和相关的服务提供商来获取“电话”和“姓名”字段。
在我尝试过的组件中:
public function onRun()
{
$this->cellphone = $this->page['cellphone'] = $this->loadCell();
}
protected function loadCell()
{
$slug = $this->propertyOrParam('idParam');
$cellphone = Cellphone::isPublished()->where('id', '=', $slug)->first();
$this->provider = $this->page['provider'] = $cellphone->provider;
return $cellphone;
}
$this->cellphone 有我想要的手机数据,但我不知道如何访问 Provider 'name' 信息。
如何从视图中的两个模型加载和访问数据?
在我看来我已经尝试过了:
<ul>
<li>{{ cellphone.phone }} {{ provider.name }} </li>
<li>Try Again </li>
<li>{{ cellphone.phone }} {{ cellphone.provider.name }} </li>
</ul>
{{ cellphone.phone }} 显示正常,但我无法获取提供商名称。
【问题讨论】:
-
您想在哪里访问提供者名称?意味着在 PHP 或视图中?
-
我编辑了我的帖子以添加我想要实现的视图。
标签: laravel-4 eloquent inner-join octobercms