Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel参考2e en 3级_Php_Laravel_Reference_Foreign Key Relationship - Fatal编程技术网

Php Laravel参考2e en 3级

Php Laravel参考2e en 3级,php,laravel,reference,foreign-key-relationship,Php,Laravel,Reference,Foreign Key Relationship,我有三张桌子 表格新闻(有许多作者) 表格编撰人(有许多新闻,属于国家) 表格国家(有许多作者) 现在我想在视图中显示它: Name(news), Name(Writer), Name(Country) Name(news), Name(Writer), country_id 我只能显示: Name(news), Name(Writer), Name(Country) Name(news), Name(Writer), country_id 如何显示国家/地区名称?在您的TableWri

我有三张桌子

表格新闻(有许多作者)

表格编撰人(有许多新闻,属于国家)

表格国家(有许多作者)

现在我想在视图中显示它:

Name(news), Name(Writer), Name(Country)
Name(news), Name(Writer), country_id
我只能显示:

Name(news), Name(Writer), Name(Country)
Name(news), Name(Writer), country_id

如何显示国家/地区名称?

在您的
TableWriter
型号中,您应该具有“国家/地区”功能

public function country() {
  return $this->belongsTo('TableWriter');
}
国家id应该是国家的
id
,您可以执行
$tableWriter->country()->name
,其中
$tableWriter
是您的
tableWriter
的一个实例

因此,通过上面的
app/TableWriter.php
中的内容,您应该能够执行以下操作:

$tableWriter = AppName\TableWriter::first();
echo $tableWriter->country()->name;

这应该与第一个TableWriter的计数器名称相呼应。

假设您有一个新闻对象,$News->writer->country->name应该可以工作。也许你的Writer模型中的一些代码会对你有所帮助,你可以在其中声明与国家/地区模型的关系。
$tableWriter = AppName\TableWriter::first();
echo $tableWriter->country()->name;