簡體   English   中英

Laravel名稱間距問題

[英]Laravel name spacing issue

我在Laravel中使用IoC容器,試圖從數據庫中獲取所有數據

<?php namespace API;

class ProductRepository implements ProductRepositoryInterface {

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

}

當我運行這個我得到這個錯誤

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

我猜想它正在嘗試在我已命名空間的API文件夾中找到產品模型。 我的問題是如何讓我的應用程序知道Product:all()應該使用Product模型。 我的名字間距正確嗎? ProductRespository位於名為API的文件夾中。

在名稱空間中時,請始終在根名稱空間中的任何類前加一個\\

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

或者,您可以在文件中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