Error Class App \ Repositories \ Messages does not exist

1

I am trying to create a repository for my queries but it tells me that the class does not exist.

I show you the code snippets to see if anyone knows what the error is:

MessagesControler.php

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Carbon\Carbon;
use App\Message;
use App\Repositories\Messages;
use App\Events\MessageWasRecived;
use Illuminate\Support\Facades\Cache;
use App\Http\Requests\CreateMessageRequest;

class MessagesController extends Controller
{
    protected $messeges;
    function __construct(Messages $messeges)
    {
        $this->middleware('auth',['except' => ['create','store']]);
        $this->messeges = $messeges;
    } 

Path of my repository app/Repositories/Messages.php

Messages.php

<?php

namespace App\Repositories;

use App\Message;
use Illuminate\Support\Facades\Cache;


class Messages 
{
    public function index()
    {
         $key = 'message.page' . request('page', 1);
            return = Cache::tags('messages')->rememberForever($key, function() {
                return Message::with(['user','note','tags'])
                                ->orderBy('created_at', request('sorted','ASC'))
                                ->paginate(10); 
            });
    }
}
    
asked by Miguel Olea 27.08.2018 в 08:04
source

1 answer

0

You have a problem with the namespaces in your project.

On the one hand you are trying to make use of the class App\Repositories\Messages , but on the other hand you have defined the class App\Repositories\Messages\Messages ( namespace App\Repositories\Messages + class Messages ), so there is no coincidence between the two.

You must remember that in use you must enter the namespace to which a class belongs plus the name of it.

You can fix it in two ways:

Using the correct class:

You can leave your Messages.php file intact and make this modification in your MessagesControler.php file:

<?php
/* ... */
use App\Message;
/* Agrego correctamente el nombre de la clase al espacio de nombres */
use App\Repositories\Messages\Messages;
use App\Events\MessageWasRecived;
/* ... */

Correcting the namespace:

You can leave the MessagesControler.php file intact and make this modification in the Messages.php file:

<?php
/* Corregimos el espacio de nombres para no repetir nombre */
namespace App\Repositories;

use App\Message;
use Illuminate\Support\Facades\Cache;
/* ... */
    
answered by 27.08.2018 в 08:37