Search code examples
phplaravelmodelstablename

laravel models, class names, table names and directory structure best practices


I was wondering what would be the best practice to create the following tables and relations in eloquent / laravel 4.1 models.

  • table name magazines with fields id, title
  • table name magazine_issues with fields id, title, magazine_id
  • table name magazine_issue_articles with fields id, title, body, magazine_issue_id

I need the file/dir structure and the naming of all the Class and files, because I am really confused, for example should I make a directory named magazines and place the Issue.php file there and like so for the articles? And what should be the name of the class itself?

Thank you in advance.

EDIT

I need to have them in subfolders, since that would be much more portable. The question is... will a class with the name magazineIssue (with table name magazine_issues) will be autoloaded and registered from a file called Issue.php in a directory named Magazine/?

I hope you are not as confused as I am :(


Solution

  • If you want to create thinks like Taylor Otwell and 'the core' is trying to teach people do things in Laravel, this is a good start:

    Your files could be organized as

    ├── app
    │   ├── Publishing
    │   │   ├── Models
    │   │   │   ├── Magazine
    │   │   │   │   ├── Magazine.php
    │   │   │   │   ├── Issue.php
    │   │   │   │   ├── Article.php
    │   │   ├── BaseModel.php
    │   │   ├── Repositories
    │   │   │   ├── MagazineRepository.php
    

    Configure a PSR-0 or PSR-4 (better) to autoload your classes:

    "psr-0": {
        "Publishing": "app/"
    },
    

    Create your namespaced BaseModel:

    <?php namespace Publishing\Models
    
    use Eloquent;
    
    class BaseModel extends Eloquent {
    
    }
    

    Create namespaced models, according to your source tree:

    <?php namespace Publishing\Models\Magazine
    
    use Publishing\Models\BaseModel;
    
    class Magazine extends BaseModel {
    
        protected $table = 'magazines';
    
    }
    
    <?php namespace Publishing\Models\Magazine
    
    use Publishing\Models\BaseModel;
    
    class Issue extends BaseModel {
    
        protected $table = 'magazines_issues';
    
    }
    
    <?php namespace Publishing\Models\Magazine
    
    use Publishing\Models\BaseModel;
    
    class Article extends BaseModel {
    
        protected $table = 'magazines_issues_articles';
    
    }
    

    You also may want to create a MagazineRepository class, to ease the access to your Magazine Domain:

    <?php namespace Publishing\Repositories;
    
    use Publishing\Models\Magazine\Magazine;
    use Publishing\Models\Magazine\Issue;
    use Publishing\Models\Magazine\Article;
    
    class MagazineRepository implements DataRepositoryInterface {
    
        private $state;
    
        public function __construct(
                                        Magazine $magazine,
                                        Issue $issue,
                                        Article $article
                                    )
        {
            $this->magazine = $magazine;
            $this->issue = $issue;
            $this->article = $article;
        }
    
        public function getAll()
        {
            return $this->magazine->all();
        }
    
        public function findArticle($articleId)
        {
            return $this->article->find($articleId);
        }
    
        public function getByArticle($articleId)
        {
            return $this->findArticle($articleId)->magazine;
        }
    
    }