Question

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 :(

Was it helpful?

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;
    }

}

OTHER TIPS

First off, remember that your tables should all be plural - magazine_issues not magazine_issue, magazine_issue_articles not magazine_issue_article.

As for directory structure, with Laravel it's kinda a do-what-you-want. A lot of people try to use namespaces and put models usually in a Models sub-namespace.

That said, your models should* match your tables names but converting the plural snake_case to singular StudlyCaps. So you should end up with three models:

class Magazine extends Eloquent
{
    public function issues()
    {
        return $this->hasMany('MagazineIssue');
    }
}

class MagazineIssue extends Eloquent
{
    public function magazine()
    {
        return $this->belongsTo('Magazine');
    }

    public function articles()
    {
        return $this->hasMany('MagazineIssueArticle');
    }
}

class MagazineIssueArticle extends Eloquent
{
    public function issue()
    {
        return $this->belongsTo('MagazineIssue');
    }
}

However, where you put them is still up to you. Just ensure that you use fully-qualified class names in your relationships if you mess round with namespaces. I'd suggest they all go in the same directory and namespace, though.

* I say you should do all this, but you are free to do what you want. The reason for the 'should' is that if you follow these conventions, Laravel will make your life a little easier.

Before going deeper into DB and table, you should have you domain layer organized around Laravel model. In your case, since you are working with issues, the name of your model should be Issue. So create new file under models directory, and place the following line inside:

class Issue extends Eloquent{};

Now you have starting point for your domain logic. Inside Issue model you can create relations and all related rules (fillable,guarded...). That is about model, I dont know have migrate anything to DB.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top