PHP code example of mmanos / laravel-metable

1. Go to this page and download the library: Download mmanos/laravel-metable 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/ */

    

mmanos / laravel-metable example snippets


use Mmanos\Metable\Metable;

class User extends Eloquent
{
	use Metable;
}

class User extends Eloquent
{
	protected $meta_model = 'Meta';
	protected $metable_table = 'user_metas';
}

class User extends Eloquent
{
	protected $metable_table_sync = ['company_id', 'created_at', 'updated_at', 'deleted_at'];
}

$user->setMeta('employer', 'Company, Inc.');

$user->setMeta([
	'employer' => 'Company, Inc.',
	'employed_for_years' => 10,
]);

$user->unsetMeta('employer');

$user->unsetMeta('employer', 'employed_for_years');
// or
$user->unsetMeta(['employer', 'employed_for_years']);

if ($user->hasMeta('employer')) {
	
}

$employer = $user->meta('employer');

$employer = $user->meta('employer', 'Unemployed');

$employer = $user->meta(['employer', 'employed_for_years']);

$metas = $user->metas;

$metas = $user->metasArray();

$users = User::withMeta('employer')->take(10)->get();

$users = User::whereMeta('employer', 'Company, Inc.')->take(10)->get();

$users = User::whereMeta('employed_for_years', '>', '5')->take(10)->get();

$users = User::whereMeta('employer', 'Company, Inc.')
	->where('meta_created_at', '>', '2015-01-01 00:00:00')
	->with('company')
	->orderBy('meta_created_at', 'desc')
	->paginate(10);

$users = User::withAnyMeta('company', 'employed_for_years')->get();
// or
$users = User::withAnyMeta(['company', 'employed_for_years'])->get();

$users = User::whereAnyMeta([
	'company' => 'Company, Inc.',
	'employed_for_years' => '10'
])->get();

// Fetch all users who have the 'agent' meta and who have 'company' or 'employed_for_years'.
$users = User::whereMeta('agent', '1')->withAnyMeta('company', 'employed_for_years')->get();

class User extends Eloquent
{
	public function metaContext()
	{
		return $this->company;
	}
}

class Meta extends Eloquent
{
	public static function applyQueryContext($query, $context)
	{
		$query->where('company_id', $context->id);
	}
	
	public static function applyModelContext($model, $context)
	{
		$model->company_id = $context->id;
	}
}

$users = User::withMeta('employer')->withMetaContext($company)->take(10)->get();
console
$ php artisan laravel-metable:metas metas
console
$ php artisan laravel-metable:metable user_metas