PHP code example of touhidurabir / laravel-meta-fields

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

    

touhidurabir / laravel-meta-fields example snippets




namespace App\Models;

use Touhidurabir\MetaFields\HasMeta;

class User extends Authenticatable {

    use HasMeta;
}

$user = User::create([...]);
$meta = $user->storeMeta([]);

/**
 * Define if a model can have multiple meta data records
 *
 * @return bool
 */
public function canHaveMultipleMetas() {

    return false;
}

/**
 * Define if the metas associated with a parent model sync with parents state
 * The sync up behaviour apply to parent's [delete, force delete and restore] events
 * 
 * @return bool
 */
public function syncWithParent() {

    return true;
}

$user->updateMeta([])

$user->deleteMeta()

$user->hasMeta()

$user->isMetaAssociated('field_name')

$user->metaValue('field_name')

use Illuminate\Foundation\Http\FormRequest;
use Touhidurabir\MetaFields\WithMetaFields;

class StoreUser extends FormRequest {

    use WithMetaFields;
    
    /**
     * The meta fields validation rules
     *
     * @return array
     */
    public function metaRules() : array {

        return [
            'bio' => [
                ...
            ],
            'dob' => [
                ...
            ],
        ];
    }
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(StoreUser $request) {
    
    // create user
    $user = User::create(...);

    // store meta for user 
    $metas = $request->metas($request->validated()); // this array[$request->validated()] argument is optional
    $user->storeMeta($metas);
}
bash
php artisan vendor:publish --provider="Touhidurabir\MetaFields\MetaFieldsServiceProvider"