实际上 Laravel 并不知道模型在哪里
如果这个例子有效:
public function user()
{
return $this->belongsTo(User::class);
}
这是因为模型可能位于同一文件夹或命名空间中,否则您可能应该像这样从其他命名空间导入所需的模型。
//At the top of the file you will import the class
use Maybe\Another\Folder\Namespace\OtherObject;
public function user()
{
return $this->belongsTo(OtherObject::class);
}
如果您不想“导入”对象,您应该像这样使用类的完整路径。
public function user()
{
return $this->belongsTo(App\OtherFolder\OtherObject::class);
}
总之,laravel 不知道在哪里查找类定义,但是如果您在参数中传递一个实例,该实例将为服务容器解析,但这是另一个与模型绑定更相关的主题
public function method(MyObject $instance)
{
//Laravel will try to automatically generate an instance of $instance
}
桃花长相依
慕慕森
牧羊人nacy