Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I namespace a model in Laravel so as not to clash with an existing class?

In Laravel 4 I want to use a model that represents an Event coming from my database. Thus, in app/models I have my Event model that extends Eloquent.

However, Laravel 4 already has an Event class which is used to manage events within the application lifecycle.

What I want to know is, how can I properly namespace my Event model and access it in a way that will not clash with the existing Event class.

like image 518
Dwight Avatar asked Apr 20 '13 03:04

Dwight


1 Answers

You just need to apply a namespace to it as you normally would. So, for example.

<?php namespace Models;

use Eloquent;

class Event extends Eloquent {

}

You should then correctly setup your composer.json so that it loads your models. You could use classmap or psr-0 for this, depending on whether or not you're following PSR-0 with your directory structure.

I'm pretty sure the models directory is already mapped.

Edit
As mentioned in the comments you must run composer dump-autoload.

like image 136
Jason Lewis Avatar answered Sep 26 '22 07:09

Jason Lewis