Class not found Laravel

1

Good afternoon, I'm starting with studies in laravel, the moment I joined Eloquent ORM I began to have problems. This is my controller.

<?php
namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use estoque\Produto;
use Request;
class ProdutoController extends Controller {

public function lista(){
  $produtos = Produto::all();
  return view('produto.listagem')->with('produtos', $produtos);
}

Classe produto 

<?php namespace estoque;
use Illuminate\Database\Eloquent\Model;

class Produto extends Model {
      protected $table = 'produtos';
  //
}

When accessing the url to perform the listing it presents the following error

  

FatalErrorException in   /Users/kleitonbatista/Documents/Projects/estoque/app/Http/Controllers/ProductoController.php   line 10: Class 'stock \ Product' not found   1.in ProductController.php line 10

Could anyone give me a light on how to solve this? Structure of the project

    
asked by anonymous 12.03.2017 / 21:55

1 answer

3

Hello! The problem is in the namespace of the product.php file and in the "Use Stock \ Product;" of the controller.

I see 2 solutions for your code compiling without errors, they are:

  • Use the default laravel file structure
  • Create a namespace to allocate your "Product.php" file (which I think is what you tried to do)
  • Solution 1 (Use standard laravel framework)

    In this larvavel solution create the models in the root of the "app" folder so that autoload works correctly you need to set the namespace of those files to reflect this structure.

    Open Product.php file and change

    DE:

      

    use stock \ Product;

    TO:

      

    App \ Product;

    After this open the App / Http / Controllers / ProductController.php file and change

    DE:

      

    use stock \ Product;

    TO:

      

    use App \ Product;

    The final code should be:

    <?php
    namespace App\Http\Controllers;
    
    use Illuminate\Support\Facades\DB;
    use App\Produto;
    use Request;
    
    class ProdutoController extends Controller {
    
        public function lista(){
            $produtos = Produto::all();
            return view('produto.listagem')->with('produtos', $produtos);
        }
    }
    

    and

    <?php 
    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    
    class Produto extends Model {
          protected $table = 'produtos';
      //
    }
    

    Solution 2 (Create an "Inventory" namespace)

    The tip is the same as the previous solution, your namespace (in this case) has to reflect the folder structure.

    If you want to create a namespace named "Stock", I suggest creating a folder inside the "App" folder with the name "Stock", then move the "Product.php" file into the newly created folder and edit the namespace of the same to "App \ Inventory" logo and then open file "ProductController" and edit the "use" section

    DE:

      

    use stock \ Product;

    To:

      

    use App \ Stock \ Product;

    The final structure would look like this:

    <?php
    namespace App\Http\Controllers;
    
    use Illuminate\Support\Facades\DB;
    use App\Estoque\Produto;
    use Request;
    
    class ProdutoController extends Controller {
    
        public function lista(){
            $produtos = Produto::all();
            return view('produto.listagem')->with('produtos', $produtos);
        }
    }
    

    and

    <?php 
    namespace App\Estoque;
    
    use Illuminate\Database\Eloquent\Model;
    
    class Produto extends Model {
          protected $table = 'produtos';
      //
    }
    

    The folder structure will look similar to this

    + estoque
       ...
       + app
         ... 
         + Http
         + Estoque
             - Produto.php
         ...
    
        
    12.03.2017 / 22:40