在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):kodeine/laravel-meta开源软件地址(OpenSource Url):https://github.com/kodeine/laravel-meta开源编程语言(OpenSource Language):PHP 100.0%开源软件介绍(OpenSource Introduction):Fluent Meta Data for Eloquent ModelsMetable Trait adds the ability to access meta data as if it is a property on your model. Metable is Fluent, just like using an eloquent model attribute you can set or unset metas. Follow along the documentation to find out more. Changelogvisit CHANGELOG.md InstallationComposerLaravel can be installed on laravel Run:
For laravel 7.x or below visit this link. Upgrade guideChange this line in
to:
after that, run Upgrade noticeLaravel meta 2 has some backward incompatible changes that listed below:
Migration Table SchemaEach model needs its own meta table. This is an example migration. you need change parts of it. In this example we assume you have a model named Meta table name should be your model's table name + If you don't want to follow this naming convention and use something else for table name, make sure you add this name to your model's body: protected $metaTable = 'custom_meta_table'; the foreign key name should be your model's name + If you used something else for foreign key, make sure you add this to your model's body: protected $metaKeyName = 'custom_foreign_key'; /**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts_meta', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('post_id')->unsigned();
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
$table->string('type')->default('null');
$table->string('key')->index();
$table->text('value')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('posts_meta');
} ConfigurationModel SetupNext, add the use Kodeine\Metable\Metable;
class Post extends Eloquent
{
use Metable;
} Metable Trait will automatically set the meta table based on your model name.
Default meta table name would be, class Post extends Eloquent
{
protected $metaTable = 'posts_meta'; //optional.
} Default Model Attribute valuesAdditionally, you can set default values by setting an array called
This is being the desired and expected functionality for most projects, but be aware that you may need to reimplement default functionality with your own custom accessors and mutators if this functionality does not fit your needs. This functionality is most suited for meta entries that note exceptions to rules. For example: employees sick out of office (default value: in office), nodes taken down for maintenance (default value: node up), etc. This means the table doesn't need to store data on every entry which is in the expected state, only those rows in the exceptional state, and allows the rows to have a default state upon creation without needing to add code to write it.
GotchaWhen you extend a model and still want to use the same meta table you must override
Working With MetaSetting Content MetaTo set a meta value on an existing piece of content or create a new data:
$post = Post::find(1);
$post->name = 'hello world'; // model attribute
$post->content = 'some content goes here'; // meta data attribute
$post->save(); // save attributes to respective tables Or $post = Post::find(1);
$post->name = 'hello world'; // model attribute
$post->setMeta('content', 'Some content here');
$post->save(); Or ...
$post->setMeta([
'content' => 'Some content here',
'views' => 1,
]);
$post->save(); Or ...
$post->setAttributes([
'name' => 'hello world'; // model attribute
'content' => 'Some content here',
'views' => 1,
]);
$post->save();
You can also save metas with $post->content = 'some content goes here'; // meta data attribute
$post->saveMeta(); // will save metas to database but won't save the model itself Unsetting Content MetaSimilarly, you may unset meta from an existing piece of content:
$post = Post::find(1);
$post->name // model attribute
unset($post->content) // delete meta on save
$post->save(); Or $post->unsetMeta('content');
$post->save(); Or $post->unsetMeta('content,views');
// or
$post->unsetMeta('content|views');
// or
$post->unsetMeta('content', 'views');
// or array
$post->unsetMeta(['content', 'views']);
$post->save();
Checking for MetasTo see if a piece of content has a meta:
if (isset($post->content)) {
}
// or
if ($post->hasMeta('content')){
} You may also check if model has multiple metas: $post->hasMeta(['content','views']); // returns true only if all the metas exist
// or
$post->hasMeta('content|views');
// or
$post->hasMeta('content,views'); Retrieving MetaTo retrieve a meta value on a piece of content, use the
$post = Post::find(1);
dump($post->name);
dump($post->content); // will access meta. Or $post = $post->getMeta('content'); Or specify a default value, if not set: $post = $post->getMeta('content', 'Something');
You may also retrieve more than one meta at a time and get an illuminate collection: // using comma or pipe
$post = $post->getMeta('content|views');
// or an array
$post = $post->getMeta(['content', 'views']);
// specify default values
$post->getMeta(['content', 'views'],['content'=>'something','views'=>0]);
// or specify one default value for all missing metas
$post->getMeta(['content', 'views'],'none');// result if the metas are missing: ['content'=>'none','views'=>'none']
// without specifying default value result will be null
$post->getMeta(['content', 'views']);// result if the metas are missing: ['content'=>null,'views'=>null] Disable Fluent AccessIf you don't want to access metas in fluent way, you can disable it by adding following property to your model: protected $disableFluentMeta = true; By setting that property, this package will no longer handle metas in the following ways: $post->content='something';// will not set meta. original laravel action will be taken
$post->content;// will not retrieve meta
unset($post->content);// will not unset meta
isset($post->content);// will not check if meta exists Retrieving All MetasTo fetch all metas associated with a piece of content, use the $metas = $post->getMeta(); Retrieving an Array of All MetasTo fetch all metas associated with a piece of content and return them as an array, use the $metas = $post->getMeta()->toArray(); Meta Table JoinWhen you need to filter your model based on the meta data , you can use $post = Post::meta()
->where(function($query){
$query->where('posts_meta.key', '=', 'revision')
->where('posts_meta.value', '=', 'draft');
}) Eager LoadingWhen you need to retrieve multiple results from your model, you can eager load $post = Post::with(['metas'])->get(); Prevent metas attribute from being populatedWhen you convert a model to an array (or json) and you don't need all meta fields, you can create a model's property to prevent metas from being added to the resulting array. You can also use it on eloquent relations. /* Post model */
public $hideMeta = true; // Do not add metas to array EventsLaravel meta dispatches several events, allowing you to hook into the following events: use Kodeine\Metable\Metable;
use Kodeine\Metable\HasMetaEvents;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use Metable,HasMetaEvents;
} After that, you can listen for events the same way you do for models.
There are 3 ways to listen for events:
1. By Defining |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论