This package for Laravel aims to provide a convenient way to use database mappings in your projects.

0.1.2 2017-05-03 07:15 UTC

This package is not auto-updated.

Last update: 2020-01-19 17:28:57 UTC


Open Source Love Build Status

Inspired by jarektkaczyk/eloquence this package for Laravel aims to provide a convenient way to use database mappings in your projects.

Currently this package isn't considered stable! Please, bear this in mind!

Suggestions, PR's are welcome!

The package requires Laravel 5.3 or newer!


Require this package with composer:

composer require rbrda/dbmapper

After Composer finishes updating you will have to add the ServiceProvider to the "providers" array in "config/app.php".


Add the Facade class to the "aliases" array (it's optional).

'DbMapper' => rBrda\DbMapper\Facades\DbMapper::class,

Last step is to copy the package config file to the "config" directory by running the following command.

php artisan vendor:publish --provider="rBrda\DbMapper\DbMapperServiceProvider"


The "config/dbmapper.php" file contains the database mappings.

You need to edit it if you want to add or remove mappings.

Query Builder

Just write your queries the same way as usual! There is nothing to add or modify, except that from now on you need to reference table names and field names by their keys as defined in the configuration file. DbMapper will automatically map them. That's all.

Here is an example for the following mapping settings:

"mysql" => [
    "articles" => [
        'table' => 'ex_articles',
        'fields' => [
            'id'       => 'art_id',
            'title'    => 'art_title',
            'category' => 'art_category',
            'tags'     => 'art_assigned_tags',

Without DbMapper:

DB::table('ex_articles')->select(['art_title', 'art_category'])->where('art_id', '=', 12)->get();

Using DbMapper:

DB::table('articles')->select(['title', 'category'])->where('id', '=', 12)->get();

Eloquent Models

Contrary to the Query Builder, Eloquent Models can define their own mappings (it's optional).

If you decide to define the mappings in your Model, follow the steps below.

  1. You need to add the following trait to your Model:

     use rBrda\DbMapper\Traits\DbMapper;
  2. Then use it in your Model class:

     use DbMapper;
  3. There are two properties that can be defined:

     /** @var string $tableMap */
     protected $tableMap = '';
     /** @var array @fieldMaps */
     protected $fieldMaps = [];

    Note: It's not mandatory to define both. If you want to map the table name only, then you can do it by defining $tableMap only.



namespace App;

use rBrda\DbMapper\Traits\DbMapper;
use Illuminate\Database\Eloquent\Model;

class Article extends Model
    use DbMapper;

    /** @var string $tableMap */
    protected $tableMap = 'ex_articles';

    /** @var array $fieldMaps */
    protected $fieldMaps = [
        'id'       => 'art_id',
        'title'    => 'art_title',
        'category' => 'art_category',
        'tags'     => 'art_assigned_tags',

Below you can see an example query using the "Article" Model.

Without DbMapper:

Article::select(['art_title', 'art_category'])->where('art_id', '=', 12)->get();

Using DbMapper:

Article::select(['title', 'category'])->where('id', '=', 12)->get();

Raw queries

Raw queries can be mapped too.

Here is an example based on the previous examples:

$tableName = dbMapper()->mapParams('articles');

$articleIdField = dbMapper()->mapParams('articles', 'id');
$titleField = dbMapper()->mapParams('articles', 'title');
$categoryField = dbMapper()->mapParaams('articles', 'category');

$result = DB::select(DB::raw("SELECT `{$titleField}`, `{$categoryField}` FROM `{$tableName}` WHERE `{$articleIdField}` = ?"), [12]);


  • Finish "README.md" (add available public functions and describe their usage)
  • Test all of the database drivers
  • Write tests