Support the ongoing development of Laravel.io →
Database Eloquent

I've making my first laravel applications based on some phpacademy tutorials on youtube. Seems like they're using Laravel 4.x and some things are a bit different but I've managed to figure them out.

Let's say I create a new model called Post (for blog posts)

By default this is the code

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model {}

If I want to use methods like ' Post::get(); ' I get the error:

"FatalErrorException in HomeController.php line 14: Class 'App\Http\Controllers\Post' not found" unless in put in the file heading: " use App\Post "

Do I have do this for every single model I create? because I've seen they don't do it in some tutorials, am I doing something wrong? or is something that you have to do in Laravel 5.

Thanks in advance

Last updated 3 years ago.
0

Hello, In order to use your Model class in controller you need to add a namespace: use App\Post; - or something like that ( depends on your structure ) :)

0

Ok so, I have to know that.

If I want to use a class in a controller I must include it: "use App\MyModel" that is better than creating an allias and populating your config\app.php file, right?

0

Alias should be only created for specific classes/models. If you will add all models to alias - you will face loading problems. I have about 70 models created in total and if I will add all of them to my app.php - i will lose performance :)

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.