PHP code example of jlopezcur / metable

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

    

jlopezcur / metable example snippets


	use Jlopezcur\Metable\MetableTrait;

	protected $meta_model       = 'ModelName'; // the name of your meta data model
	protected $meta_foreign_key = 'user_id'; // the foreign key of your main model
	protected $meta_primary_key = 'meta_id'; // the primary key of you meta data model
	protected $meta_key_name    = 'dataName'; // the column name that stores your meta data key name
	protected $meta_value_name  = 'dataValue'; // the column name that stores your meta data value

class User extends Eloquent {

	use Jlopezcur\Metable\MetableTrait;

	protected $meta_model       = 'UserMeta';
	protected $meta_foreign_key = 'user_id';
	protected $meta_primary_key = 'id';
	protected $meta_key_name    = 'meta_name';
	protected $meta_value_name  = 'meta_value';


	/**
	 * The database table used by the model.
	 *
	 * @var string
	 */
	protected $table = 'users';

	/**
	 * The primary key on the table
	 *
	 * @var string
	 */
	protected $primaryKey = 'id';

	// ...
}

class UserMeta extends Eloquent {

	/**
	 * The database table used by the model.
	 *
	 * @var string
	 */
	protected $table = 'users_meta';

	/**
	 * The primary key on the table
	 *
	 * @var string
	 */
	protected $primaryKey = 'id';

	// ...
}

$user = User::find(1);
echo $user->gender; // Will output 'Male'

$user = User::find(1);
$user->gender = 'Female';
$user->save();

$user = User::find(1);
$user->gender = null;
$user->save();

$user = User::find(1);
$user->anything_you_want = 'some lovely value';
$user->save();

$user = User::find(1);
$user->fill($request->input())->save();
$user->save();

protected $guarded = array();