### Install kodeine/laravel-meta via Composer Source: https://github.com/kodeine/laravel-meta/blob/2.0/README.md This command installs the kodeine/laravel-meta package using Composer, a dependency manager for PHP. Ensure you are running Laravel 8.x or higher. ```bash composer require kodeine/laravel-meta ``` -------------------------------- ### Listen to metaCreated Event using Observers Source: https://github.com/kodeine/laravel-meta/blob/2.0/README.md This example illustrates how to listen for the `metaCreated` event using an observer class in Laravel. The `UserObserver` class contains a `metaCreated` method that will be triggered when a meta is created for a User model. ```php class UserObserver { public function metaCreated(User $user,$meta) { // } } ``` -------------------------------- ### Listen to metaCreated Event using Closures Source: https://github.com/kodeine/laravel-meta/blob/2.0/README.md This example shows how to listen for the `metaCreated` event using a closure within the `booted` static method of a Laravel Eloquent model. This approach allows for inline event handling logic directly within the model class. ```php use Kodeine\Metable\Metable; use Kodeine\Metable\HasMetaEvents; use Illuminate\Database\Eloquent\Model; class User extends Model { use Metable,HasMetaEvents; protected static function booted() { static::metaCreated(function ($user, $meta) { // }); } } ``` -------------------------------- ### Create Meta Table Migration (SQL/PHP) Source: https://github.com/kodeine/laravel-meta/blob/2.0/README.md This code snippet provides an example of a Laravel migration to create a meta table for Eloquent models. It defines the schema for storing meta data, including foreign key constraints and fields for type, key, and value. The table name convention is `model_table_name_meta` and the foreign key is `model_id`. ```PHP 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'); } } ``` -------------------------------- ### Listen to metaSaved Event using $dispatchesEvents Source: https://github.com/kodeine/laravel-meta/blob/2.0/README.md This example demonstrates how to listen for the `metaSaved` event by defining the `$dispatchesEvents` property within a Laravel Eloquent model. This allows you to specify a custom event class, `UserMetaSaved`, to be dispatched when metas are saved. ```php use App\Events\UserMetaSaved; use Kodeine\Metable\Metable; use Kodeine\Metable\HasMetaEvents; use Illuminate\Database\Eloquent\Model; class User extends Model { use Metable,HasMetaEvents; protected $dispatchesEvents = [ 'metaSaved' => UserMetaSaved::class, ]; } ``` -------------------------------- ### Upgrade kodeine/laravel-meta package Source: https://github.com/kodeine/laravel-meta/blob/2.0/README.md Instructions to upgrade the kodeine/laravel-meta package to version ^2.0. This involves changing the version constraint in composer.json and running composer update. Note the backward incompatible changes in version 2. ```bash "kodeine/laravel-meta": "master" ``` ```bash "kodeine/laravel-meta": "^2.0" ``` ```bash composer update ``` -------------------------------- ### Retrieve All Metas Source: https://github.com/kodeine/laravel-meta/blob/2.0/README.md Demonstrates how to retrieve all meta values associated with a model by calling the `getMeta` method without any parameters. The result is an Illuminate Collection. ```php $metas = $post->getMeta(); ``` -------------------------------- ### Eager Loading Metas Source: https://github.com/kodeine/laravel-meta/blob/2.0/README.md Explains how to eager load the `metas` relationship when retrieving multiple model instances to avoid the N+1 query problem and improve performance. ```php $posts = Post::with(['metas'])->get(); ``` -------------------------------- ### Retrieve All Metas as Array Source: https://github.com/kodeine/laravel-meta/blob/2.0/README.md Shows how to fetch all meta values for a model and convert them into a plain PHP array using the `toArray` method on the result of `getMeta()`. ```php $metas = $post->getMeta()->toArray(); ``` -------------------------------- ### Retrieve Multiple Meta Values Source: https://github.com/kodeine/laravel-meta/blob/2.0/README.md Illustrates retrieving multiple meta values at once, either by using a comma-separated string, a pipe-separated string, or an array of keys. It also covers specifying default values for missing metas. ```php $post = $post->getMeta('content|views'); $post = $post->getMeta(['content', 'views']); $post = $post->getMeta(['content', 'views'],['content'=>'something','views'=>0]); $post = $post->getMeta(['content', 'views'],'none'); $post = $post->getMeta(['content', 'views']); ``` -------------------------------- ### Retrieve Single Meta Value Source: https://github.com/kodeine/laravel-meta/blob/2.0/README.md Demonstrates how to retrieve a single meta value from an Eloquent model using the `getMeta` method. It also shows how to provide a default value if the meta key does not exist. ```php $post = Post::find(1); dump($post->name); $metaContent = $post->getMeta('content'); $metaContentWithDefault = $post->getMeta('content', 'Something'); ``` -------------------------------- ### Enable Meta Events Source: https://github.com/kodeine/laravel-meta/blob/2.0/README.md Demonstrates how to enable meta-related events by using the `HasMetaEvents` trait in the Eloquent model. This allows listeners to hook into events like `metaCreating`, `metaCreated`, etc. ```php use Kodeine\Metable\Metable; use Kodeine\Metable\HasMetaEvents; use Illuminate\Database\Eloquent\Model; class User extends Model { use Metable, HasMetaEvents; } ``` -------------------------------- ### Access Original Attribute Methods Source: https://github.com/kodeine/laravel-meta/blob/2.0/README.md Explains how to use `getAttributeRaw` and `setAttributeRaw` to access the original Laravel `getAttribute` and `setAttribute` methods when fluent meta access is enabled. If fluent access is disabled, these methods behave identically to the original ones. ```php $post->getAttributeRaw('content'); $post->setAttributeRaw('content', 'new value'); ``` -------------------------------- ### Filter Models by Meta Data using `whereMeta` Scope Source: https://github.com/kodeine/laravel-meta/blob/2.0/README.md Provides a simpler way to filter models by meta data using the `whereMeta` scope, which accepts the meta key, value, an optional alias, and an optional operator. ```php Post::whereMeta($key, $value, $alias = null, $operator = '=')->get(); ``` -------------------------------- ### Use Metable Trait in Eloquent Model (PHP) Source: https://github.com/kodeine/laravel-meta/blob/2.0/README.md This PHP code shows how to integrate the Metable trait into an Eloquent model. By using `use Metable;`, the model gains the functionality to manage meta data fluently, treating meta attributes like regular model properties. ```PHP use Kodeine\Metable\Metable; class Post extends Eloquent { use Metable; } ``` -------------------------------- ### Set Multiple Meta Values at Once in Laravel Source: https://github.com/kodeine/laravel-meta/blob/2.0/README.md Efficiently set multiple meta values for a Laravel Eloquent model using an array with the `setMeta` method. This allows for batch updates to meta data before saving the model. ```php $post->setMeta([ 'content' => 'Some content here', 'views' => 1, ]); $post->save(); ``` -------------------------------- ### Filter Models by Meta Data using `meta` Scope Source: https://github.com/kodeine/laravel-meta/blob/2.0/README.md Illustrates how to use the `meta` scope in the Eloquent Query Builder to filter models based on their associated meta data, allowing conditions on meta keys and values. ```php Post::meta() ->where(function($query){ $query->where('posts_meta.key', '=', 'revision') ->where('posts_meta.value', '=', 'draft'); }) ``` -------------------------------- ### Set Multiple Attributes and Meta Values in Laravel Source: https://github.com/kodeine/laravel-meta/blob/2.0/README.md Update both model attributes and meta data simultaneously using the `setAttributes` method. This provides a convenient way to manage different types of data associated with a model in a single operation. ```php $post->setAttributes([ 'name' => 'hello world', // model attribute 'content' => 'Some content here', 'views' => 1, ]); $post->save(); ``` -------------------------------- ### Check for Multiple Meta Existence in Laravel Source: https://github.com/kodeine/laravel-meta/blob/2.0/README.md Verify if a Laravel Eloquent model possesses multiple meta values. The `hasMeta` method can accept a string of keys separated by pipes or commas, or an array, returning true only if all specified meta keys exist. ```php $post->hasMeta(['content','views']); // returns true only if all the metas exist // or $post->hasMeta('content|views'); // or $post->hasMeta('content,views'); ``` -------------------------------- ### Prevent Metas from Populating Array/JSON Output Source: https://github.com/kodeine/laravel-meta/blob/2.0/README.md Shows how to prevent the `metas` attribute from being included when a model is converted to an array or JSON by setting a public property `$hideMeta = true;` in the model. ```php public $hideMeta = true; ``` -------------------------------- ### Set Default Meta Values in Laravel Model Source: https://github.com/kodeine/laravel-meta/blob/2.0/README.md Define default values for meta attributes on a Laravel model by setting the `$defaultMetaValues` static property. If a meta attribute doesn't exist, its default value is returned. Setting a meta attribute to its default value removes the corresponding row from the meta table. ```php class YourModel extends Model { public $defaultMetaValues = [ 'is_user_home_sick' => false, ]; } ``` -------------------------------- ### Disable Fluent Meta Access Source: https://github.com/kodeine/laravel-meta/blob/2.0/README.md Shows how to disable the fluent meta access feature by adding a `protected $disableFluentMeta = true;` property to the Eloquent model. This prevents meta operations from overriding standard attribute access. ```php protected $disableFluentMeta = true; ``` -------------------------------- ### Set Single Meta Value using setMeta in Laravel Source: https://github.com/kodeine/laravel-meta/blob/2.0/README.md Use the `setMeta` method to assign a meta value to a Laravel Eloquent model. This method is useful for explicitly managing meta data attributes. Changes are saved when the model's `save()` method is invoked. ```php $post = Post::find(1); $post->setMeta('content', 'Some content here'); $post->save(); ``` -------------------------------- ### Check for Single Meta Existence in Laravel Source: https://github.com/kodeine/laravel-meta/blob/2.0/README.md Determine if a Laravel Eloquent model has a specific meta value. This can be checked using the `isset` construct directly on the attribute or by using the `hasMeta` method, which provides a more explicit check. ```php if (isset($post->content)) { } // or if ($post->hasMeta('content')){ } ``` -------------------------------- ### Set Custom Meta Table Name in Model (PHP) Source: https://github.com/kodeine/laravel-meta/blob/2.0/README.md This PHP code demonstrates how to specify a custom meta table name within an Eloquent model. By defining the protected property `$metaTable`, you can override the default naming convention used by the Metable trait. ```PHP class Post extends Eloquent { protected $metaTable = 'custom_meta_table'; //optional. } ``` -------------------------------- ### Set Single Meta Value in Laravel Source: https://github.com/kodeine/laravel-meta/blob/2.0/README.md Set a meta value for a Laravel Eloquent model. If the attribute is a model attribute, it's set directly; otherwise, it's managed via the meta table. The changes are persisted when the model's `save()` method is called. ```php $post = Post::find(1); $post->content = 'some content goes here'; // meta data attribute $post->save(); ``` -------------------------------- ### Override getMetaKeyName in Laravel Model Extension Source: https://github.com/kodeine/laravel-meta/blob/2.0/README.md When extending a model that uses the meta table, override the `getMetaKeyName` method to specify the correct foreign key name for the meta table relationship. This ensures that meta data is correctly associated with the parent model. ```php class Slideshow extends Post { protected function getMetaKeyName() { return 'post_id'; // The parent foreign key } } ``` -------------------------------- ### Unset Multiple Meta Values in Laravel Source: https://github.com/kodeine/laravel-meta/blob/2.0/README.md Remove multiple meta values from a Laravel Eloquent model efficiently. The `unsetMeta` method accepts a comma-separated string, a pipe-separated string, multiple arguments, or an array of meta keys to be removed upon saving the model. ```php $post->unsetMeta('content,views'); // or $post->unsetMeta('content|views'); // or $post->unsetMeta('content', 'views'); // or array $post->unsetMeta(['content', 'views']); $post->save(); ``` -------------------------------- ### Save Meta Data Without Saving Model in Laravel Source: https://github.com/kodeine/laravel-meta/blob/2.0/README.md Persist meta data changes to the database independently of the main model's save operation using the `saveMeta` method. This is useful when only meta data needs to be updated. ```php $post->content = 'some content goes here'; // meta data attribute $post->saveMeta(); // will save metas to database but won't save the model itself ``` -------------------------------- ### Unset Single Meta Value using unsetMeta in Laravel Source: https://github.com/kodeine/laravel-meta/blob/2.0/README.md Use the `unsetMeta` method to remove a specific meta value from a Laravel Eloquent model. The change is applied when the model's `save()` method is called. The system does not error if the meta does not exist. ```php $post->unsetMeta('content'); $post->save(); ``` -------------------------------- ### Unset Single Meta Value in Laravel Source: https://github.com/kodeine/laravel-meta/blob/2.0/README.md Remove a meta value from a Laravel Eloquent model. When the model is saved, the specified meta data will be deleted from the database. This can be done using the `unset` keyword or the `unsetMeta` method. ```php $post = Post::find(1); unset($post->content) // delete meta on save $post->save(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.