David Hurtado Banda
David Hurtado Banda

Reputation: 11

Laravel Repository class not found

I got the following problem. I started to move all the database logic to the repositories, but when I call the repository from the controller it gives me an error: "Class App\Repositories\TransactionRepository does not exist".

I tried to fix it doing "composer dump-autoload", "composer install", "composer update", "php artisan cache:clear"

I started creating a repository at 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();
        }
    }

Then I call it from the proper 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');
    }

And here is my composer.json:

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

If someone have any idea I'll be so glad.

Upvotes: 0

Views: 2075

Answers (1)

David Hurtado Banda
David Hurtado Banda

Reputation: 11

Im so happy to put an alternative solution that it works for me.

So different from another answers I saw in similar post, I found this:

At composer.json add the classmap : "app/Repositories"

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

Enjoy it like I do ;)

Upvotes: 1

Related Questions