PHP code example of toriomlab / eloquent-form-elements
1. Go to this page and download the library: Download toriomlab/eloquent-form-elements 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/ */
toriomlab / eloquent-form-elements example snippets
namespace App;
use Illuminate\Database\Eloquent\Model;
use ToriomLab\EloquentFormElements\Traits\FormGenerator;
class Fee extends Model
{
use FormGenerator;
}
public static $fields = [
'field_name_attribute' => [ // Field name attribute should be the column name in DB as well to retreive its value in update form generation.
'label' => 'Field Label Text', // Field Label.
'input' => 'input', // Field Type.
'type' => 'text' // Field Input Type.
],
];
'name' => [
'label' => 'Your name',
'input' => 'input',
'type' => 'text'
],
'age' => [
'label' => 'Your age',
'input' => 'input',
'type' => 'number'
],
'avatar' => [
'label' => 'Label Name',
'element' => 'img',
'image_classes' => 'img-rounded img-thumbnail img-responsive',
'storage_folder' => 'storage/courses/',
],
'line' => [
'element' => 'hr'
],
'role_id' => [
'label' =>'Role',
'input' => 'select',
'relation' => [
'model' => 'App\Role',
'type' => 'one',
'column' => 'role_id',
'selectFrom' => 'name',
'valueFrom' => 'id',
// This is how selectFrom and valueFrom works
// <option value="{{ $role->id }}">{{ $role->name }}</option>
],
],
'roles[]' => [
'label' => 'Roles',
'input' => 'select',
'relation' => [
'name' => 'roles',
'model' => 'App\Role',
'type' => 'many',
'selectFrom' => 'name',
'valueFrom' => 'id'
],
'inject_attributes' => 'multiple'
],
namespace App;
use Illuminate\Database\Eloquent\Model;
use ToriomLab\EloquentFormElements\Traits\FormGenerator;
class User extends Model
{
use FormGenerator;
public static $fields = [
'name' => [
'label' => 'Your name',
'input' => 'input',
'type' => 'text',
],
'age' => [
'label' => 'Your age',
'input' => 'input',
'type' => 'number',
],
'roles[]' => [
'label' => 'Roles',
'input' => 'select',
'relation' => [
'name' => 'roles',
'model' => 'App\Role',
'type' => 'many',
'selectFrom' => 'name',
'valueFrom' => 'id',
],
'inject_attributes' => 'multiple'
],
];
public function roles()
{
return $this->belongsToMany('App\Role');
}
}
public static $fields = [
'exception_ids[]' => [
'label' => 'المنتجات المستثناة',
'input' => 'select',
'valueCallback' => 'getExceptionsValues',
'valueFallback' => 'getAllProducts',
'selectFrom' => 'name_ar',
'valueFrom' => 'id',
'inject_attributes' => 'multiple'
],
];
public function getExceptionsValues()
{
$results = [];
$exception_ids = $this->exception_ids ? json_decode($this->exception_ids) : [];
foreach ($exception_ids as $exception_id) {
$product = Product::find($exception_id);
$results[] = $product;
}
return $results;
}
public static function getAllProducts()
{
return Product::latest()->get();
}
/**
* The attributes that are building the model forms.
*
* @var array
*/
public static $fields = [
'name' => [
'label' => 'Category Name',
],
'category_id' => [
'label' => 'Categories',
'input' => 'select',
'options' => [
'' => 'All Categories',
],
'selectFrom' => 'name',
'valueFrom' => 'id',
'valueCallback' => 'getCurrentValue', // Can be replaced with 'column' => 'category_id' for belongsTo relation
'updateValueFallback' => 'getUpdateCategories',
'createValueFallback' => 'getAllCategories',
],
];
/**
* Get dropdown categories for EloquentFormElements.
*
* @return self
*/
public function getUpdateCategories()
{
return static::where('id', '!=', $this->id)->get();
}
/**
* Get all dropdown categories for EloquentFormElements Creation.
*
* @return self
*/
public static function getAllCategories()
{
return static::latest()->get();
}
/**
* Get Current value for the manual relation.
* @return mixed
*/
public function getCurrentValue()
{
return $this->category_id;
}