Skip to content

Commit 5310362

Browse files
authored
Update README.md
1 parent bdea3a4 commit 5310362

File tree

1 file changed

+92
-51
lines changed

1 file changed

+92
-51
lines changed

README.md

Lines changed: 92 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Dynamic Model for Laravel!
1+
# Dynamic Model for Laravel
22

33
[![Latest Version on Packagist](https://img.shields.io/packagist/v/laracraft-tech/laravel-dynamic-model.svg?style=flat-square)](https://packagist.org/packages/laracraft-tech/laravel-dynamic-model)
44
[![Tests](https://github.com/laracraft-tech/laravel-dynamic-model/actions/workflows/run-tests.yml/badge.svg?branch=main)](https://github.com/laracraft-tech/laravel-dynamic-model/actions/workflows/run-tests.yml)
@@ -11,7 +11,8 @@ Normally, each model in Laravel is written for only one table, and it's not so e
1111
This is for a good reason - it ensures a well-designed and clean model.
1212
But in very specific cases, you may need to handle multiple tables via a single model.
1313
Here **Laravel Dynamic Model** comes into play!
14-
It provides you with an eloquent model which finally can handle multiple database tables!
14+
It provides you with an eloquent model which finally can handle multiple tables
15+
and if you want also multiple database connections!
1516

1617
## Installation
1718

@@ -33,133 +34,173 @@ composer require laracraft-tech/laravel-dynamic-model
3334

3435
### Let's create some dummy tables:
3536

37+
---
38+
3639
``` bash
3740
php artisan make:migration create_foo_table
38-
php artisan make:migration create_faz_table
3941
php artisan make:migration create_bar_table
40-
php artisan make:migration create_baz_table
4142
```
4243

43-
Create migrations for all tables, for example:
44+
Create migrations for the tables:
4445

4546
``` php
46-
...
4747
Schema::create('foo', function (Blueprint $table) {
4848
$table->id();
4949
$table->string('col1');
5050
$table->integer('col2');
5151
$table->timestamps();
5252
});
53-
...
53+
54+
Schema::create('bar', function (Blueprint $table) {
55+
$table->date('period')->primary();
56+
$table->string('col1');
57+
$table->integer('col2');
58+
$table->timestamps();
59+
});
5460
```
5561

5662
``` bash
5763
php artisan migrate
5864
```
5965

60-
### Let's use the Dynamic Model:
61-
**Note**: You could also use a different database connection.
62-
Therefor just pass the `db_connection` as a second parameter.
66+
### Let's create a Dynamic Model
6367

64-
Also be aware that the default DynamicModel is set to **unguarded**.
65-
If you do not like this, check the section below and create your own dynamic model.
68+
---
69+
70+
If you want to create a **Dynamic Model** then you have to use the **DynamicModelFactory**.
71+
The Factory ensures, that the `table` and optionally the `connection` gets set for your new created model.
72+
Also it checks the schema of your provided table to set the propper values for: `primaryKey`, `keyType`, `incrementing`.
73+
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.
74+
75+
Note that the default DynamicModel is set to **unguarded**.
76+
If you do not like this or you want your Dynamic Models have some custom functions,
77+
check the section below and create your own Dynamic Model.
6678

6779
``` php
6880
use LaracraftTech\LaravelDynamicModel\DynamicModel;
81+
use LaracraftTech\LaravelDynamicModel\DynamicModelFactory;
6982

70-
$foo = App::make(DynamicModel::class, ['foo']);
83+
$foo = app(DynamicModelFactory::class)->create(DynamicModel::class, 'foo');
7184

7285
$foo->create([
7386
'col1' => 'asdf',
7487
'col2' => 123
7588
]);
7689

77-
$faz = App::make(DynamicModel::class, ['faz']);
78-
$faz->create([...]);
79-
80-
$bar = App::make(DynamicModel::class, ['bar']);
81-
$bar->create([...]);
90+
$faz = app(DynamicModelFactory::class)->create(DynamicModel::class, 'faz');
8291

83-
$baz = App::make(DynamicModel::class, ['baz']);
84-
$baz->create([...]);
92+
$faz->create([
93+
'period' => '2023-01-01',
94+
'col1' => 'asdf',
95+
'col2' => 123
96+
]);
8597

86-
// use another db connection (this one must be defined in your config/database.php file)
87-
$fooOtherDB = App::make(DynamicModel::class, ['baz', 'your-optional-db-connection-name-here']);
98+
// optionally use another db connection (this one must be defined in your config/database.php file)
99+
$fooOtherDB = app(DynamicModelFactory::class)->create(DynamicModel::class, 'foo', 'mysql2');
88100
$fooOtherDB->create([...]);
89101

90-
dd($foo->first());
102+
dump($foo->first());
103+
dump($faz->first());
104+
dump($fooOtherDB->first());
91105
```
92106

93-
Which gives us:
107+
Which gives you:
94108

95109
```
96-
^ LaracraftTech\LaravelDynamicModel\DynamicModel {#328 ▼
97-
...
110+
^ LaracraftTech\LaravelDynamicModel\DynamicModel_mysql_foo {#328 ▼
111+
#connection: "mysql"
112+
#table: "foo"
113+
#primaryKey: "id"
114+
#keyType: "int"
115+
+incrementing: true
116+
#attributes: array:5 [▼
117+
"id" => 1
118+
"col1" => "asdf"
119+
"col2" => 123
120+
"created_at" => "2023-03-22 15:34:22"
121+
"updated_at" => "2023-03-22 15:34:22"
122+
]
123+
}
124+
125+
^ LaracraftTech\LaravelDynamicModel\DynamicModel_mysql_faz {#328 ▼
126+
#connection: "mysql"
127+
#table: "faz"
128+
#primaryKey: "period"
129+
#keyType: "string"
130+
+incrementing: false
131+
#attributes: array:5 [▼
132+
"period" => "2023-01-01"
133+
"col1" => "asdf"
134+
"col2" => 123
135+
"created_at" => "2023-03-22 15:34:22"
136+
"updated_at" => "2023-03-22 15:34:22"
137+
]
138+
}
139+
140+
^ LaracraftTech\LaravelDynamicModel\DynamicModel_mysql2_foo {#328 ▼
141+
#connection: "mysql2"
142+
#table: "foo"
143+
#primaryKey: "id"
144+
#keyType: "int"
145+
+incrementing: true
98146
#attributes: array:5 [▼
99147
"id" => 1
100148
"col1" => "asdf"
101149
"col2" => 123
102150
"created_at" => "2023-03-22 15:34:22"
103151
"updated_at" => "2023-03-22 15:34:22"
104152
]
105-
...
106153
}
107154
```
108155

109-
### Use your own dynamic model
156+
### Use your own Dynamic Model
110157

111-
If you need to add methods to a dynamic model,
112-
you can just create your own Eloquent model and **extend** it by the **DynamicModel**
113-
or if the model is already extended by another model, you can just use the **DynamicModelBinding** trait.
158+
---
114159

115-
If you use the trait, make sure your model implements the **DynamicModelInterface**.
116-
Also make sure to always resolve these models through the container!
160+
If you need to add **custom** methods to your Dynamic Model or maybe **guard** it,
161+
you can just create your own Eloquent model and then call it through the **Factory**.
162+
This will create a new Model instance, which is **extended** by your original model.
163+
Make sure your model is implementing the **DynamicModelInterface** or is extended by the **DynamicModel** class.
117164

118165
``` php
119166
namespace App\Models;
120167

121168
use Illuminate\Database\Eloquent\Model;
122169
use LaracraftTech\LaravelDynamicModel\DynamicModel;
123-
use LaracraftTech\LaravelDynamicModel\DynamicModelBinding;
124170
use LaracraftTech\LaravelDynamicModel\DynamicModelInterface;
125171

126-
# option 1: use extends
172+
// class MyDynamicModel extends Model implements DynamicModelInterface (this would also work)
127173
class MyDynamicModel extends DynamicModel
128174
{
129-
public function doSomething()
130-
{
131-
// do something
132-
}
133-
}
134-
135-
# option 2: use the trait when class already extends a class
136-
class MyDynamicModel extends SomeBaseModel implements DynamicModelInterface
137-
{
138-
use DynamicModelBinding;
139-
140-
protected $guarded = [];
175+
proteced $guarded = ['id'];
141176

142177
public function doSomething()
143178
{
144179
// do something
145180
}
146181
}
147182

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

150185
$foo->create([
151186
'col1' => 'asdf',
152187
'col2' => 123
153188
]);
154189

190+
$foo->doSomething();
191+
155192
dd($foo->first());
156193
```
157194

158-
Which gives us:
195+
Which gives you:
159196

160197
```
161-
^ App\Model\MyDynamicModel {#328 ▼
162-
...
198+
^ App\Model\MyDynamicModel_mysql_foo {#328 ▼
199+
#connection: "mysql"
200+
#table: "foo"
201+
#primaryKey: "id"
202+
#keyType: "int"
203+
+incrementing: true
163204
#attributes: array:5 [▼
164205
"id" => 1
165206
"col1" => "asdf"

0 commit comments

Comments
 (0)