Laravel: conflict between model name and inline facade

I have a model in my Laravel application called Event

. As I just discovered, this creates a conflict between my model and the Illuminate\Support\Facades\Event

embedded facade. The obvious solution here is either to change the name of my model, which is not ideal because there is really no other name I could give my model, which makes sense, or rename the alias to app.php

for Illuminate\Support\Facades\Event

, which I would like to avoid for fear of breaking anything that can rely on this alias in the future (I'm afraid I'll forget).

It has been suggested that perhaps I could use the namespaces I tried like this:

applications / models / Event.php

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

class Event extends \Eloquent{

      

applications / databases / seeds / DatabaseSeeder.php

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

      

All 4 combinations above gave an error Class 'Models\Event' not found

when I ran php artisan db:seed

.

Perhaps I am just misunderstanding the namespaces, but the more pressing issue is the solution to my problem. If this can be solved with namespaces as suggested, great, but I'm open to any other ideas too.

+3


source to share


1 answer


I made this mistake early on, not necessarily understanding the role of the namespace in the entire application.

The namespace should mark business logic within a domain or the responsibility of the application itself, so providing a model namespace is not necessarily useful. Instead, create a root namespace listed after the application, your company, you, or whatever, and then provide the Model submenu space.

For example:

namespace MyGreatApp\Models;

class Event extends \Eloquent{ }

      



Then you have to reference this model under:

use MyGreatApp\Models\Event;

$event = new Event();

      

In the long run, this is a cleaner and more organized approach. This means that you are moving your models to a different folder. But there is nothing wrong with that. At least you know that you have your code in your MyGreatApp namespace. :)

+3


source







All Articles