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:
- Use standard filestructure of the Laravel
- 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
...