php – How and when to create packages in Laravel 4?


I've been researching and thinking about a subject for days and I still don't understand how to do/use/start:


  • Package: Books (crud)
  • Package: Authentication Module (users + profiles + login + permission)


I have 2 packages, but packaeg Books belongs to (belongsTo) User: how do I do this?

  1. How do I set up this relationship? (different packages)
  2. How do I configure a package dependency ? (Book dependent on user to work properly)
  3. And if the authentication package doesn't exist, but the default files in the APP folder, how do I relate to the Books package?
  4. Do books and authentication really need to be packages? In other words: is this the real concept of packages? small modules, 1 library (upload for example), etc.)

It's more of a conceptual question than a practical one, but I would like to know in practice how to effect relationships as well.

If you need more information let me know.



Good Morning!

If I understood correctly what your doubts were:

Relationships, in laravel, are stored in models.

Model Book, for example:

public function user()

If you want the relationship to be called every time you retrieve one or more books from the database, you can use the with attribute of the model Eloquent\Model:

protected $with = ['user'];

where 'user' is the name given in the method above.

Laravel has great permissions management, so you could define more rules in the User model, like:

public function isAdmin() { return ($this->isAdmin == true); }

So you can check if the user is admin anywhere in the application, just calling:


Following the same course, you can make validations more complex and flexible using a table to store rules or groups as well.

Permissions management example used by Django in sqlite, without the restrictions:

CREATE TABLE `auth_user` (
    `id`    integer NOT NULL,
    `password`  varchar(128) NOT NULL,
    `last_login`    datetime NOT NULL,
    `is_superuser`  bool NOT NULL,
    `group_id`  integer NOT NULL,
    `username`  varchar(30) NOT NULL UNIQUE,
    `first_name`    varchar(30) NOT NULL,
    `last_name` varchar(30) NOT NULL,
    `email` varchar(75) NOT NULL,
    `is_active` bool NOT NULL,
    `date_joined`   datetime NOT NULL,

CREATE TABLE `auth_permission` (
    `id`    integer NOT NULL,
    `name`  varchar(50) NOT NULL,
    `content_type_id`   integer NOT NULL,
    `codename`  varchar(100) NOT NULL,

CREATE TABLE `auth_group_permissions` (
    `id`    integer NOT NULL,
    `group_id`  integer NOT NULL,
    `permission_id` integer NOT NULL,

CREATE TABLE `auth_group` (
    `id`    integer NOT NULL,
    `name`  varchar(80) NOT NULL UNIQUE,

That way you could do an eager loading

to determine if a user has permission to access a particular location, along with adding filters that do this automatically.

The packages are intended to make the project more organized, flexible and reusable, so that you can use them in other applications without many changes. Its use is not necessary, but recommended for large applications.

Instead of building packages, you could create a folder inside /app called library and add to composer.json:

"autoload": {
        "classmap": [
        "psr-0": {
            "Biblioteca": "app/biblioteca"

For each script inside this folder, namespace separation must be used.

A good example is the open source of laravel-tricks .

Scroll to Top