dpg76975 2014-01-26 19:13
浏览 29
已采纳

Laravel名称间距问题

I am using the IoC container in Laravel, I am trying to grab all the data from my database

<?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. My question is how do I get my application to know that Product:all() should use the Product model. Is my name spacing correct? ProductRespository is sitting inside a folder called API.

  • 写回答

1条回答 默认 最新

  • doulianqi3870 2014-01-26 19:16
    关注

    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:

    <?php namespace API;
    
    use Product;
    
    class ProductRepository implements ProductRepositoryInterface {
    
        public function getProducts()
        {
            return Product::all();
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据