5

I have a Model in my Laravel app called Event. As I just discovered, this creates a conflict between my model and Illuminate\Support\Facades\Event, a built-in facade. The obvious solution here is to either change the name of my Model, which is not ideal because there is really no other name I could give my Model that makes any sense, or to rename the alias in app.php for Illuminate\Support\Facades\Event, which I'd like to avoid for fear of breaking anything that may rely on that alias in the future (I'm afraid I may forget).

It's been suggested that perhaps I could use namespaces, which I attempted as follows:

app/models/Event.php

namespace Models; #<-- I've also tried using "\Models" here

class Event extends \Eloquent{

app/database/seeds/DatabaseSeeder.php

Models\Event::create();  #<-- again, I've also used "\Models\Event"

All 4 combinations above have yielded a Class 'Models\Event' not found error when I run php artisan db:seed.

Perhaps I simply don't understand namespaces properly, but the more pressing issue is how to solve my problem. If it can be solved using namespaces as suggested, great, but I'm open to any other ideas as well.

ewok
  • 20,148
  • 51
  • 149
  • 254
  • I think you are over complicating it. Just call your model something else. Event "EventModel" might be ok. – Laurence Aug 03 '14 at 01:31
  • Yeah, that's what I did for now. But at this point, I really want to know if this is possible to work out. Namespaces are an interesting option. I'd rather call all my models by their Laravel-expected name and put them in the `Models` namespace then rename them all and have it not line up. – ewok Aug 03 '14 at 01:37

1 Answers1

4

I made this mistake early on as well, not necessarily understanding the role of namespace throughout the entire app.

The namespace should mark the business logic within the domain or responsibility of the app itself, so giving a namespace of Models isn't necessarily useful. Instead create a root namespace named after the app, your company, you, or whatever you like, then provide a Model sub-namespace.

For example:

namespace MyGreatApp\Models;

class Event extends \Eloquent{ }

Then you would reference this model under:

use MyGreatApp\Models\Event;

$event = new Event();

In the long run this is a cleaner and more organized approach. This does mean moving your models into a different folder, though. But there's nothing wrong with that. At least that way you know you have all your custom code in your MyGreatApp namespace. :)

mike.bronner
  • 1,203
  • 1
  • 20
  • 39
  • (1) is it possible to reference the Model like this: `MyGreatApp\Models\Event::create()`? (2) is there functionally any difference between this and simply defining a `Models` namespace, or is it just a design difference? (3) do I need to move the rest of my app into the `MyGreatApp` namespace? If so, how do I do this? I haven't used PHP in a few years so I'm new to PHP namespaces – ewok Aug 03 '14 at 02:26
  • 2
    1) Yes. Using the use ...; statement is just creating an alias so you don't have to write it out all the time. 2) Both. It keeps your code separate from the framework, letting you move it around much easier, also leads to cleaner coding and better organization in the app. 3) No, only move your custom code into that folder. At first you will probably start out with only small projects that hardly have any code in your namespace, but as you remove all the logic out of the controllers, you will create classes in your namespace for that. :) Welcome back to PHP, I underwent the same journey. – mike.bronner Aug 03 '14 at 02:53
  • 1
    Thanks. As it turns out, the reason my code wasn't working was I needed to run `composer dump-autoload` after defining my namespace. – ewok Aug 03 '14 at 03:10
  • Good point, I should have mentioned that, but didn't think you were getting that far. Use composer dump-autoload -o for optimized building of the autoload file. It will speed things up later when your app gets large! :) Also, if you haven't seen http://laracasts.com yet, they're are probably the single best resource to learning laravel on the internet. Completely worth it. – mike.bronner Aug 03 '14 at 03:13