1

I have created a php file under the Models folder provided by Laravel.

Inside I have:

// Filename: Models/Shoopy.php
namespace Whatever\Something;

class Shoopy
{
    public function Toot(){};
}

Now in another Model file I am using that namespace:

use \Whatever\Something\Shoopy;

class AnotherModel
{
    public function Spop()
    {
        $harr = new Shoopy();
    }
}

It comes up with a Laravel error:

class Whatever\Something\Shoopy not found

Any ideas?

2 Answers 2

2

I know this is an older issue, but if you go into your composer.json file, you'll probably see something like...

"autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App\\": "app/"
    }
},

What you can do is tell composer where Whatever should be, say if it was a different location (or sub-folder) from where Laravel normally keeps it's files.

"autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App\\": "app/",
        "Whatever\\": "app/Whatever",
    }
},
Sign up to request clarification or add additional context in comments.

Comments

1

Classes need to exist in files with directory paths that match their namespace. This is according to the PSR-0 Standard. In your example, try placing your Shoopy class in the file:

app/models/Whatever/Something/Shoopy.php

And then see if the autoloader can find it.

5 Comments

oh... That's a bit annoying.
It may be, but it was even more annoying back when there was absolutely no structure or standards whatsoever. You can always remove the namespace and leave the model where it is.
The problem is, the real Model name is Pagination, so it's likely there will be many plugins called Pagination, so just wanted to stamp a namespace on it... Oh well, looks like I should put it into composer in that case, then I can do it as I wanted
If the class doesn't extend Eloquent, then it most likely doesn't really belong in the models folder. I generally add a directory underneath app/ for all of my 'helper' or service classes, and use that as a base namespace (you'd need to update your composer.json in order to get the autoloader to find that namespace)
I generally put all classes under the Models folder into separate folders, Abstract, Concrete, Objects, Eloquent, Helpers and auto load them in the config file... Thanks for the recommendation though. I use Models as the generic term for anything that is not a Controller or View

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.