PHP code example of miladev / lara-meta

1. Go to this page and download the library: Download miladev/lara-meta library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

miladev / lara-meta example snippets


Miladev\LaravelMeta\MetaServiceProvider::class,

use \Illuminate\Database\Eloquent\Model;
use \Miladev\LaravelMeta\Metable;

class Post extends Model
{
    use Metable;
}

$post = Post::withMetas()->first();

$post = Post::first();
$post->metas;

$post = Post::first();
$post->saveMeta('meta_key_here', 'value_here');
$post->getMeta('meta_key_here', 'default_value');
$post->updateMeta('meta_key_here', 'value_here_new');
$post->deleteMeta('meta_key_here');
$post->findMeta('value_here');

class Post extends Model
{
    use Metable;

    protected $metaCasts = [
        'views' => 'int',
        'rating' => 'float',
        'active' => 'bool',
        'data' => 'json',
        'published_on' => 'date',
    ];
}

$post->saveMeta('views', '100');
$post->getMeta('views');       // 100 (int)

$post->saveMeta('active', true);
$post->getMeta('active');      // true (bool)

$post->saveMeta('data', ['a' => 1]);
$post->getMeta('data');        // ['a' => 1] (array)

$post->saveMeta('published_on', Carbon::parse('2024-01-15'));
$post->getMeta('published_on'); // DateTime object
bash
php artisan vendor:publish --provider="Miladev\LaravelMeta\MetaServiceProvider"
bash
php artisan migrate