Laravel 参考文献 2e 和第 3 级
Laravel References 2e en 3th level
我有 3 张桌子。
Table news (hasMany Writers)
id
name
Table 作家(有很多新闻和属于国家/地区)
id
Name
country_id
Table 个国家(有许多作家)
id
name
现在我想在视图中显示它:
Name(news), Name(Writer), Name(Country)
我只能显示:
Name(news), Name(Writer), country_id
如何显示国家名称?
在您的 TableWriter
模型中,您应该有一个函数 'country'。
public function country() {
return $this->belongsTo('TableWriter');
}
country_id 应该是国家的 id
,你可以做 $tableWriter->country()->name
,其中 $tablewriter
是你的 TableWriter
的一个实例。
因此,在 app/TableWriter.php
中,您应该可以执行以下操作:
$tableWriter = AppName\TableWriter::first();
echo $tableWriter->country()->name;
这应该与第一个 TableWriter 的计数器名称相呼应。
我有 3 张桌子。
Table news (hasMany Writers)
id
name
Table 作家(有很多新闻和属于国家/地区)
id
Name
country_id
Table 个国家(有许多作家)
id
name
现在我想在视图中显示它:
Name(news), Name(Writer), Name(Country)
我只能显示:
Name(news), Name(Writer), country_id
如何显示国家名称?
在您的 TableWriter
模型中,您应该有一个函数 'country'。
public function country() {
return $this->belongsTo('TableWriter');
}
country_id 应该是国家的 id
,你可以做 $tableWriter->country()->name
,其中 $tablewriter
是你的 TableWriter
的一个实例。
因此,在 app/TableWriter.php
中,您应该可以执行以下操作:
$tableWriter = AppName\TableWriter::first();
echo $tableWriter->country()->name;
这应该与第一个 TableWriter 的计数器名称相呼应。