Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.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';中引用对象属性;从一个模型来看,控制器扮演什么角色?_Php_Laravel - Fatal编程技术网

Php 如何在Laravel';中引用对象属性;从一个模型来看,控制器扮演什么角色?

Php 如何在Laravel';中引用对象属性;从一个模型来看,控制器扮演什么角色?,php,laravel,Php,Laravel,我在比较拉威尔的两张表。模型的类中包含以下函数: public function sampleTableReference() { return $this->hasOne(samepleTableReference::class, foreignKey:'otherColumnId', localKey:'ColumnId'); } 我们认为: <?php dd($collection->sampleTableReference()->dd

我在比较拉威尔的两张表。模型的类中包含以下函数:

  public function sampleTableReference() {
        return $this->hasOne(samepleTableReference::class, foreignKey:'otherColumnId', localKey:'ColumnId');
    }
我们认为:

<?php dd($collection->sampleTableReference()->dd())?>
30是我需要引用的正确数字,但在
示例表\u reference
中有一个相邻的列,即
'names'

如何在视图中引用此内容

类似于:
$view\u collection->sampleReference()->names

??你的问题有点令人困惑,因为我认为你可能误用了一些术语,但基本概念是
$model->relationship->field
。如果要创建查询生成器以添加条件,则只能将
()
添加到关系的末尾。除非迭代每个模型,否则您将无法调用集合上的关系。您的意思是,迭代集合中的每个值吗?我的意思是,如果它是一个模型集合,则需要逐个迭代每个模型。但从示例的上下文来看,有迹象表明,
$collection
变量实际上可能是一个具有误导性名称的模型。示例是一个集合。我不知道这样使用收藏是否可行。我也无法在视图中显示关系。如果
$collection
实际上是一个集合,则您的
dd(…)
示例将抛出错误,而不是返回SQL查询。如果你至少不了解基本知识,我不确定我能帮你什么忙。我建议你看看,这是一个很好的开始方式。
"select * from `sample_table_reference` where `sample_table_reference`.`columnId` = ? and `sample_table_references`.`columnId` is not null and `sample_table_reference`.`deleted_at` is null
array:1 [▼
  0 => 30
]