未找到Laravel Repository类

时间:2016-05-02 13:50:22

标签: repository laravel-5.2

我遇到了以下问题。我开始将所有数据库逻辑移动到存储库,但是当我从控制器调用存储库时,它给出了一个错误:" Class App \ Repositories \ TransactionRepository不存在"。

我尝试修复它" composer dump-autoload"," composer install"," composer update"," php artisan cache:clear& #34;

我开始在App / Repositories / TransactionRepository.php

创建一个存储库
 <?php

    namespace App\Repositories;

    use Finance\Transaction;
    use Illuminate\Support\Facades\Auth;
    use Illuminate\Support\Facades\DB;

    class TransactionRepository
    {
        /**
         * @param $date
         */
        public function byDate($date)
        {
            return Transaction::select(DB::raw('*'))
                ->where('user_id', '=', Auth::user()->id)
                ->where(DB::raw('DATE(datetime)'), '=', DATE('Y-m-d', strtotime($date)))
                ->get();
        }
    }

然后我从正确的TransactionController.php

中调用它
<?php

namespace Finance\Http\Controllers;

use Finance\Category;
use Illuminate\Support\Facades\Cache;
use Session;
use Redirect;
use Finance\Transaction;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Finance\Http\Requests;
use Illuminate\Support\Facades\Auth;
use App\Repositories\TransactionRepository;

class TransactionController extends Controller
{

    protected $TransactionRepo;

    /**
     * TransactionController constructor.
     */
    public function __construct(TransactionRepository $transactionRepository)
    {
        $this->TransactionRepo = $transactionRepository;
        $this->middleware('auth');
    }

这是我的composer.json:

"autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "Finance\\": "app/"
    }
},

如果有人有任何想法,我会很高兴。

1 个答案:

答案 0 :(得分:0)

我很高兴为我提供一个替代解决方案。

与我在类似帖子中看到的另一个答案有所不同,我发现了这个:

在composer.json中添加classmap:&#34; app / Repositories&#34;

"autoload": {
  "classmap": [
    "app/Repositories"
  ]
}

像我一样享受它;)