简体   繁体   English

Laravel名称间距问题

[英]Laravel name spacing issue

I am using the IoC container in Laravel, I am trying to grab all the data from my database 我在Laravel中使用IoC容器,试图从数据库中获取所有数据

<?php namespace API;

class ProductRepository implements ProductRepositoryInterface {

  public function getProducts()
  {
    return Product::all();
  }

}

When I run this I get this error 当我运行这个我得到这个错误

Symfony \ Component \ Debug \ Exception \ FatalErrorException
Class 'API\Product' not found

I am guessing it is trying to find the Product model in the API folder which I have namespaced. 我猜想它正在尝试在我已命名空间的API文件夹中找到产品模型。 My question is how do I get my application to know that Product:all() should use the Product model. 我的问题是如何让我的应用程序知道Product:all()应该使用Product模型。 Is my name spacing correct? 我的名字间距正确吗? ProductRespository is sitting inside a folder called API. ProductRespository位于名为API的文件夹中。

When in a namespace, always prepend a \\ to any class in the root namespace: 在名称空间中时,请始终在根名称空间中的任何类前加一个\\

public function getProducts()
{
     return \Product::all();
}

Alternatively, you can use the class in your file: 或者,您可以在文件中use该类:

<?php namespace API;

use Product;

class ProductRepository implements ProductRepositoryInterface {

    public function getProducts()
    {
        return Product::all();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM