我试图使用存储库,但我收到此错误:
Class App\Repositories\CategoryRepository does not exist
这是我的CategoryRepository.php
<?php
namespace App\Repositories;
class SubCate
{
/**
* Get all of the tasks for a given user.
*
* @param User $user
* @return Collection
*/
public function getCategories(){
$categories=\App\category::where('parent_id',0)->get();//united
$categories=$this->addRelation($categories);
return $categories;
}
}
?>
这是我的控制者:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\Product;
use App\Category;
use App\Repositories\CategoryRepository;
class ProductController extends Controller
{
//
public function __construct(CategoryRepository $categoryRepository)
{
$this->categoryRepository = $categoryRepository;
}
public function index(Request $request)
{
$subcate = new SubCate;
try {
$allSubCategories=$subcate->getCategories();
} catch (Exception $e) {
//no parent category found
}
return view('welcome', [
'allSubCategories' => $allSubCategories,
]);
}
}
有什么问题?
答案 0 :(得分:1)
您的类别存储库类名称是
class SubCate
但你正在使用
use App\Repositories\CategoryRepository;
。
因此,请将您的班级名称更改为CategoryRepository