我想创建自定义存储库类,因此我创建了扩展抽象类的存储库类(使用基础,模型方法):
的 BoardCategoryRepository
namespace App\Repositories\Board;
use App\Repositories\Repository;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
class BoardCategoryRepository extends Repository
{
public function getAllCategoriesWithBoards(): Collection
{
return DB::table('board_category')
->join('board', 'board_category.id', '=', 'board.category_id')
->get()
;
}
}
的HomeController
namespace Community\Http\Controllers;
use App\Repositories\Board\BoardCategoryRepository;
use Community\BoardCategory;
use function compact;
use Illuminate\View\View;
class HomeController extends Controller
{
/**
* @var BoardCategoryRepository
*/
protected $boardCategoryRepository;
public function __construct(BoardCategory $boardCategory)
{
$this->boardCategoryRepository = new BoardCategoryRepository($boardCategory);
}
}
和composer dump-autoload
但我收到错误Class 'App\Repositories\Board\BoardCategoryRepository' not found
- 为什么?
答案 0 :(得分:0)
您的存储库的命名空间是App\Repositories\Board
,但您的控制器的命名空间是Community\Http\Controllers
。它实际上应该是Community\Http\Controllers
。
或者,检查您的composer.json并查找PSR-4
部分检查,看看App是否在那里注册。例如;
"psr-4": {
"App\\": "app/"
}