Giter Club home page Giter Club logo

laravel-dynamic-model's Introduction

Dynamic Model for Laravel

Latest Version on Packagist Tests Check & fix styling License

Normally, each model in Laravel is written for only one table, and it's not so easy to break this convention. This is for a good reason - it ensures a well-designed and clean model. But in very specific cases, you may need to handle multiple tables via a single model. Here Laravel Dynamic Model comes into play! It provides you with an eloquent model which finally can handle multiple tables and if you want also multiple database connections!

Installation

Dependencies

This package depends on Doctrine/DBAL, so make sure you have it installed.

composer require doctrine/dbal

Package

composer require laracraft-tech/laravel-dynamic-model

Usage

Let's create some dummy tables:


php artisan make:migration create_foo_table
php artisan make:migration create_bar_table

Create migrations for the tables:

Schema::create('foo', function (Blueprint $table) {
    $table->id();
    $table->string('col1');
    $table->integer('col2');
    $table->timestamps();
});

Schema::create('bar', function (Blueprint $table) {
    $table->date('period')->primary();
    $table->string('col1');
    $table->integer('col2');
    $table->timestamps();
});
php artisan migrate

Let's create a Dynamic Model


If you want to create a Dynamic Model then you have to use the DynamicModelFactory. The Factory ensures, that the table and optionally the connection gets set for your new created model. Also it checks the schema of your provided table to set the propper values for: primaryKey, keyType, incrementing. Means, also if you defined your table schema to have a primary key called for instance period with a date type, the Factory will handle it for you.

Note that the default DynamicModel is set to unguarded. If you do not like this or you want your Dynamic Models have some custom functions, check the section below and create your own Dynamic Model.

use LaracraftTech\LaravelDynamicModel\DynamicModel;
use LaracraftTech\LaravelDynamicModel\DynamicModelFactory;

$foo = app(DynamicModelFactory::class)->create(DynamicModel::class, 'foo');

$foo->create([
    'col1' => 'asdf',
    'col2' => 123
]);

$faz = app(DynamicModelFactory::class)->create(DynamicModel::class, 'faz');

$faz->create([
    'period' => '2023-01-01',
    'col1' => 'asdf',
    'col2' => 123
]);

// optionally use another db connection (this one must be defined in your config/database.php file)
$fooOtherDB = app(DynamicModelFactory::class)->create(DynamicModel::class, 'foo', 'mysql2');
$fooOtherDB->create([...]);

dump($foo->first());
dump($faz->first());
dump($fooOtherDB->first());

Which gives you:

^ LaracraftTech\LaravelDynamicModel\DynamicModel_mysql_foo {#328 ▼
  #connection: "mysql"
  #table: "foo"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #attributes: array:5 [▼
    "id" => 1
    "col1" => "asdf"
    "col2" => 123
    "created_at" => "2023-03-22 15:34:22"
    "updated_at" => "2023-03-22 15:34:22"
  ]
}

^ LaracraftTech\LaravelDynamicModel\DynamicModel_mysql_faz {#328 ▼
  #connection: "mysql"
  #table: "faz"
  #primaryKey: "period"
  #keyType: "string"
  +incrementing: false
  #attributes: array:5 [▼
    "period" => "2023-01-01"
    "col1" => "asdf"
    "col2" => 123
    "created_at" => "2023-03-22 15:34:22"
    "updated_at" => "2023-03-22 15:34:22"
  ]
}

^ LaracraftTech\LaravelDynamicModel\DynamicModel_mysql2_foo {#328 ▼
  #connection: "mysql2"
  #table: "foo"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #attributes: array:5 [▼
    "id" => 1
    "col1" => "asdf"
    "col2" => 123
    "created_at" => "2023-03-22 15:34:22"
    "updated_at" => "2023-03-22 15:34:22"
  ]
}

Use your own Dynamic Model


If you need to add custom methods to your Dynamic Model or maybe guard it, you can just create your own Eloquent model and then call it through the Factory. This will create a new Model instance, which is extended by your original model. Make sure your model is implementing the DynamicModelInterface or is extended by the DynamicModel class.

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use LaracraftTech\LaravelDynamicModel\DynamicModel;
use LaracraftTech\LaravelDynamicModel\DynamicModelInterface;

// class MyDynamicModel extends Model implements DynamicModelInterface (this would also work)
class MyDynamicModel extends DynamicModel
{
    proteced $guarded = ['id'];

    public function doSomething()
    {
        // do something
    }
}

$foo = app(DynamicModelFactory::class)->create(MyDynamicModel::class, 'foo');

$foo->create([
    'col1' => 'asdf',
    'col2' => 123
]);

$foo->doSomething();

dd($foo->first());

Which gives you:

^ App\Model\MyDynamicModel_mysql_foo {#328 ▼
  #connection: "mysql"
  #table: "foo"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #attributes: array:5 [▼
    "id" => 1
    "col1" => "asdf"
    "col2" => 123
    "created_at" => "2023-03-22 15:34:22"
    "updated_at" => "2023-03-22 15:34:22"
  ]
  ...
}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

laravel-dynamic-model's People

Contributors

dependabot[bot] avatar github-actions[bot] avatar sairahcaz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

savage-demon

laravel-dynamic-model's Issues

Add connection to allow different database then default

It would be nice to add connection(table_connection?) as option to DynamicModel to allow use of a different database then the default one.
My dynamic tables are in a different table then the default to make it easier to separate them :)

Unable to use geography types

Hello, I'm using Postgresql with PostGIS enabled and have a table which contains a column of type geography.

Im getting this error below:

Doctrine\DBAL\Exception Unknown database type geography requested, Doctrine\DBAL\Platforms\PostgreSQL100Platform may not support it.

May I know if it's supported to use geography types or am I doing something wrong?

Defning belongsTo Relation in Dynamic Model

Hi
I have a dynamic model (dataInFile) which extends DynamicModel. This model has a reverse one-to-many relation with another model (UploadedFile).

class DataInFile extends DynamicModel
{
    use HasFactory;
    public function file(){
        return $this->belongsTo(UploadedFile::class);
    }
}

class UploadedLists extends Model
{
    use HasFactory;
    public function fileData(): HasMany
    {
        $foo = app(DynamicModelFactory::class)->create(DataInFile::class, 'data_1_1');
        return $this->hasMany($foo, 'fileId');
    }
}

When I access the DataInFile model with

 $foo = app(DynamicModelFactory::class)->create(DataInFile::class, 'data_1_1');
 $foo->find(1);

it works fine and I am able to get the uploadedFile.
However the reverse does not work, error being Base table or view not found: 1146 Table 'my_data_base.data_in_file' doesn't exist

So is there a way to be able to do this?

Also to make it a little more complicated the name of the table depends on a column in the table UploadedLists .

Is this something that can be achieved.

Any help is highly appreciated

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.