|
|
|
@ -26,129 +26,10 @@ Add the following provider to config/app.php
|
|
|
|
|
]
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Getting Started
|
|
|
|
|
For information on getting started and full code documentation, visit the wiki:
|
|
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
|
|
When declaring a new model, extend the Meta model:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App;
|
|
|
|
|
|
|
|
|
|
class Post extends Meta
|
|
|
|
|
{
|
|
|
|
|
// fillable, hidden, etc. arrays
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
> *Important*: do not extend the Model class, Meta does this for you
|
|
|
|
|
|
|
|
|
|
The Meta plugin adds a custom Artisan command to auto-generate migrations for meta-enabled models.
|
|
|
|
|
To generate a migration for a new meta-model, use the following command:
|
|
|
|
|
|
|
|
|
|
``` bash
|
|
|
|
|
$ php artisan meta:migration name_of_migration --create=table_name
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
> (The `meta:migration` command extends the `make:migration`, so it contains all of the original functionality.)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Add your custom fields, if any, then migrate as usual:
|
|
|
|
|
|
|
|
|
|
``` bash
|
|
|
|
|
$ php artisan migrate
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Then, just create a model like usual:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
public function createPost () {
|
|
|
|
|
$post = Post::create([
|
|
|
|
|
// post data
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
But now, you have access to a universal identifier for every model in the entire project.
|
|
|
|
|
|
|
|
|
|
You can check if a model has a UUID, and get it if it does:
|
|
|
|
|
``` php
|
|
|
|
|
$post->hasUuid(); // returns true or false
|
|
|
|
|
$post->getUuid(); // returns the UUID
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
The other big feature of the Meta model is saving meta information.
|
|
|
|
|
This information is stored in the database in one column: 'meta'
|
|
|
|
|
|
|
|
|
|
To save meta information, use the 'write' method.
|
|
|
|
|
``` php
|
|
|
|
|
$post->write('author', 'Bob Saget');
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
And to retrieve meta information, use the 'read' method.
|
|
|
|
|
``` php
|
|
|
|
|
$post->read('author'); //returns 'Bob Saget'
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Adding Meta functionality to existing models
|
|
|
|
|
|
|
|
|
|
To add Meta capabilities to existing models, you need to add the database columns and tweak the model's class as follows:
|
|
|
|
|
|
|
|
|
|
##### Database update
|
|
|
|
|
|
|
|
|
|
Create a new database migration using the custom Meta command:
|
|
|
|
|
|
|
|
|
|
``` bash
|
|
|
|
|
$ php artisan meta:migration --table=existing_table_name
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Make sure the inside of the `up()` function contains the `formTable` function:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
public function up()
|
|
|
|
|
{
|
|
|
|
|
Schema::table('existing_table_name', function (Blueprint $table) {
|
|
|
|
|
Meta::formTable($table);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Then, migrate:
|
|
|
|
|
|
|
|
|
|
``` bash
|
|
|
|
|
$ php artisan migrate
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
##### Model Class updates
|
|
|
|
|
|
|
|
|
|
To enable the database functionality, the class for the model needs to extend the Meta class. Modify the class header to the following format:
|
|
|
|
|
|
|
|
|
|
``` php
|
|
|
|
|
|
|
|
|
|
class ClassNameHere extends Meta {
|
|
|
|
|
// your class stuff here
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
> *Important*: you no longer need to extend the Model class, Meta does this for you
|
|
|
|
|
|
|
|
|
|
After that, you can use the full meta functionality with your model.
|
|
|
|
|
|
|
|
|
|
### Query Builder
|
|
|
|
|
|
|
|
|
|
Let's say you need to retrieve the post that has a certain UUID (maybe you gave it out in a url or something)
|
|
|
|
|
|
|
|
|
|
It's super easy to search for it:
|
|
|
|
|
``` php
|
|
|
|
|
Post::withUuid($uuid)->first();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
It is completely integrated into the query builder, so you can still do stuff like:
|
|
|
|
|
``` php
|
|
|
|
|
Post::withUuid($uuid)->pluck('username');
|
|
|
|
|
Post::withUuid($uuid)->firstOrFail();
|
|
|
|
|
```
|
|
|
|
|
[https://github.com/glmdev/meta/wiki](https://github.com/glmdev/meta/wiki)
|
|
|
|
|
|
|
|
|
|
## Credits
|
|
|
|
|
|
|
|
|
|