1

I have a laravel 4.1 application, and I've created a folder in my app folder to store most of the logic.

/app/Acme/Models/
/app/Acme/Repositories/

these are the two main folders.

In my composer.json I have this in the auto load, and done a dump run.

    "psr-4" : {
            "Acme\\" : "app/Acme"
    }

However I am getting, what I think are silly issues. For example my Acme/Models/Task.php has the following

<?php
namespace Acme\Models;

class Task extends \Eloquent {

    public function job()
    {
        return $this->belongsTo('Job');
    }

}

however when I run this, I get an error

Fatal error: Class 'Task' not found in vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 780

In my Job.php I have the same namespace at the top of the file....

Must I manually import/use object which are in the same name space? use Acme/Models/Job as Job; ? it seems like such a duplicate...

And in my Repositories folder when I set a namespace of namespace Acme/Repositories;, must I use items like

use Acme\Models\Job as Job;

I is, a bit lost!

1 Answer 1

2

Namespaces are relative so you do not need to add use to directly reference classes within the same namespace.

The error you are getting is because you need to fully qualify relationships to namespaced models so eloquent knows where to find them eg

$this->belongsTo('\Acme\Models\Jobs');

In the case of your repository namespace, you will need to add a use statement in your file as you suggested, or reference the fully qualified namespace eg new \Acme\Models\Job();

On a side note, I know PHPStorm (and I'm sure other IDEs) will inject the namespaces for you which is super useful and saves you having to write use or the full namespace out every time you reference a class - worth checking out.

Edit: Sorry, I didn't read the question properly first time around - updated my answer.

Sign up to request clarification or add additional context in comments.

2 Comments

Jude, I thought because in Task.php I was using namespace Acme\Models; I would be able to just use Job since it's in the same scope...?
Sorry I skimmed over the line saying they were in the same NS and assumed you meant the other class was in Acme\Repository NS. You're right again, it should be relative however the error you're getting is from a completely different issue... You just need to fully qualify the relationship. eg $this->belongsTo('\Acme\Models\Job');

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.