[英]Laravel - Returning an Eloquent Instance Syntax
So I have a controller with a constructor that uses Reflection to inject a Eloquent Model dependency: 所以我有一个带有构造函数的控制器,该构造函数使用Reflection注入Eloquent Model依赖项:
public function __construct(Analysis $analysis)
{
$this->analysis = $analysis;
}
This dependency also has other Eloquent Model dependencies that are injected via Reflection: 此依赖项还具有通过反射注入的其他Eloquent Model依赖项:
use App\Load;
use App\Parameter;
class Analysis extends Model
{
protected $loads;
protected $parameters;
public function __construct(Load $loads, Parameter $parameters)
{
$this->loads = $loads;
$this->parameters = $parameters;
}
}
When I dump or die like so: 当我这样转储或死亡时:
dd($this->analysis['loads']);
It returns Load as an Eloquent instance (as I would like it to). 它返回Load作为Eloquent实例(正如我希望的那样)。
But then when I try dump or die like so: 但是,当我尝试像这样转储或死亡时:
dd($this->analysis->loads);
It just returns an empty collection. 它只是返回一个空集合。 I can't figure out why one works and not the other?
我不知道为什么一个有效,而另一个无效? I've seen the latter syntax work in tutorial videos.
我在教程视频中看到了后一种语法的工作。
I wasn't sure what to put in the post title because I don't know the terms of the differing syntax (analysis['loads'] vs analysis->loads). 我不确定要在帖子标题中加上什么,因为我不知道不同语法的术语(analysis ['loads'] vs analysis-> loads)。
So changing the loads & parameters properties to public
meant that 因此,将载荷和参数属性更改为
public
意味着
dd($this->analysis->loads);
Would then return an Eloquent instance, as desired. 然后将根据需要返回一个Eloquent实例。 Probably not the best solution.
可能不是最好的解决方案。 I guess the correct way would be to use getters and setters...
我猜正确的方法是使用getters和setters ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.