Autoload of Models in Laravel 5.1

2

Good evening I wonder if in Laravel 5.1 there is a way to do autoload, both in Models and Controllers? In Laravel 4 to create an instance of a Model, simply call the Model, like this:

$user = new User();

But from what I noticed in Laravel 5, you need to put the Model path in the controller.

    
asked by anonymous 16.09.2015 / 00:16

2 answers

1

You have to use use and the path to models ou controllers ,

example:

File ProductController.php, I'm getting the product model like this:

 use app\Produto;
    
16.09.2015 / 00:28
1

The autoload is still there, it just depends how you want to call the class

Normal

namespace App\Http\Controllers;

class UsersController extends App\Http\Controllers\Controller {

    public function create() {
        $user = new App\User;
    }

}

Or creating an alias

Aliasing

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\User;

class UsersController extends Controller {

    public function create() {
        $user = new User();
    }

}

To review a review on the basics of laravel 5, I recommend seeing link is free.

    
16.09.2015 / 06:31