Class not found Laravel

Asked

Viewed 6,457 times

2

Good afternoon, I’m starting with my studies in Laravel, the moment I entered the Eloquent ORM I started having 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 displays the following error

Fatalerrorexception in /Users/kleitonbatista/Documents/Projects/stock/app/Http/Controllers/Productocontroller.php line 10: Class 'stock Product' not found 1.in Productocontroller.php line 10

Could someone give me some light on how to fix this? Project structure Estrutura do projeto

1 answer

4


Hello! The problem is in the namespace of the.php product file and the controller’s "use Product;" stock.

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

  1. Use standard filestructure of the Laravel
  2. Create a namespace to allocate your "Product.php" file (which I think is what you tried to do)

Solution 1 (Use standard Laravel structure)

In this solution Aravel creates the models at the root of the "app" folder so for autoload to work properly you need to set the namespace of these files so that it reflects this structure.

Open Product.php file and change

OF:

use stock Product;

FOR:

App Product;

After that, open the App/Http/Controllers/Productocontroller.php file and change it

OF:

use stock Product;

FOR:

use App Product;

The final code shall 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 a namespace "Stock")

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

If you want to create a namespace called "Stock", I suggest creating a folder inside the folder "App" with in the name of "Stock", then move the file "Product.php" inside the newly created folder and edit the namespace of the same to "Stock App" soon and then open file "Productocontroller" and edits the "use" section"

OF:

use stock Product;

To:

use App Stock Product;

The final structure would be as follows:

<?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';
  //
}

Folder structure will look similar to this

+ estoque
   ...
   + app
     ... 
     + Http
     + Estoque
         - Produto.php
     ...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.