I am trying to autoload models in laravel 9,
I want to use models in my blade files, but when i use a model i have to import it writing "use AppModelsUser
", and after i can write "{{ User::all() }}
", but if i dont import the model first it gives me this error: "Class "User" not found
", so i want to write "{{ User::all() }}
" without importing the model first (without writing this: "use AppModelsUser
") in the blade file. How can i autoload them?
2
Answers
Just like you do in all other Laravel versions, for example:
Where
User
class needs to define what "posts" means, like:You can manually alias things in
config/app.php
.This will make
User
available without having to manually writeAppModelsUser
every time in the view and everywhere else.Just like you can write
instead of
You will be able to write
use User
instead ofuse AppModelsUser
(though I do not recommend you to do this).