{"id":20961979,"url":"https://github.com/lilessam/eloquentfields","last_synced_at":"2025-03-13T07:12:30.178Z","repository":{"id":62517130,"uuid":"104879682","full_name":"lilessam/EloquentFields","owner":"lilessam","description":"Eloquent Models Fields Generator","archived":false,"fork":false,"pushed_at":"2017-12-19T15:29:07.000Z","size":15,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-20T00:53:24.420Z","etag":null,"topics":["eloquent","eloquent-models","eloquent-orm","forms","generator"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lilessam.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-09-26T12:06:51.000Z","updated_at":"2023-07-16T20:54:01.000Z","dependencies_parsed_at":"2022-11-02T13:31:11.658Z","dependency_job_id":null,"html_url":"https://github.com/lilessam/EloquentFields","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lilessam%2FEloquentFields","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lilessam%2FEloquentFields/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lilessam%2FEloquentFields/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lilessam%2FEloquentFields/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lilessam","download_url":"https://codeload.github.com/lilessam/EloquentFields/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243358306,"owners_count":20277995,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["eloquent","eloquent-models","eloquent-orm","forms","generator"],"created_at":"2024-11-19T02:19:38.772Z","updated_at":"2025-03-13T07:12:30.159Z","avatar_url":"https://github.com/lilessam.png","language":"PHP","readme":"### Eloquent-Fields Generator Package For Laravel\nThis package actually contains a helper function and a trait.\nIts basic target is to reduce time that developers spend writing Creation \u0026 Update forms markup for Eloquent Models.\n\nIt's basic idea is to use the `FormGenerator` trait in your model and write a tiny array with your fields' preferences. Then you can use the helper function to generate a creation or update form.\n\n#### Installing.\n###### Via composer\n```shell\ncomposer require lilessam/eloquent-fields dev-master\n```\nThen in your model use the trait `Lilessam\\EloquentFields\\Traits\\FormGenerator` like the following:\n```PHP\n\u003c?php\nnamespace App;\n\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Lilessam\\EloquentFields\\Traits\\FormGenerator;\n\nclass Fee extends Model\n{\n    use FormGenerator;\n}\n\n```\n\n#### How to use it?\nBy defining a static array in your model called `$fields` and its structure should be as following example.\n```PHP\npublic static $fields = [\n    'field_name_attribute' =\u003e [ // Field name attribute should be the column name in DB as well to retreive its value in update form generation.\n        'label'    =\u003e 'اسم الحقل', // Field Label.\n        'input'    =\u003e 'input', // Field Type.\n        'type'     =\u003e 'text'  // Field Input Type.\n    ],\n];\n```\nThis will generate a bootstrap `form-group` div contains this field.\nThen you can generate a creation form fields in your view files like the following example:\n```HTML\n\u003cform class=\"form-horizontal\" method=\"post\"\u003e\n    {!! generate_fields('App\\Fee') !!}\n    {!! csrf_field() !!}\n    \u003cbutton type=\"submit\" class=\"btn btn-primary\"\u003eحفظ!\u003c/button\u003e\n\u003c/form\u003e\n```\nAnd you can create an update form fields by passing a second parameter contains the object id.\n```HTML\n\u003cform class=\"form-horizontal\" method=\"post\"\u003e\n    {!! generate_fields('App\\Fee', 1) !!}\n    {!! csrf_field() !!}\n    \u003cbutton type=\"submit\" class=\"btn btn-primary\"\u003eحفظ!\u003c/button\u003e\n\u003c/form\u003e\n```\nSo this actually will generate an update form fields for the row which its id is 1.\n\n#### Full Guide\n##### $fields array writing rules.\n1. Must provide `label`.\n2. Must provide `input` which can be `input`, `select` or `textarea`.\n\nIf the `input` index is `input` you must provide a `type` index to provide the input type like `text` or `number` or `date` etc.\n\nIf the `input` type is `select` you can provide an `options` index which will contain an array for the select dropdown options. Or you can provide a `relation` index which will get options from a relation in the eloquent model.\n\n##### Relation array\nThere's three types of Eloquent Relatioships supported till now in the package. You have to provide a `type` index in the relation array to specify the relation type based on the following:\n1. `belongsTo` its type will be `one` because we will only select one option in this case.\n2. `hasMany` Or `belongsToMany`: their type will be `many` because we will select multiple values.\n\nIf the `type` index in the relation array is `one` you will need to provide\n* `model` index which will be the relation model.\n* `column` which is the foreign key.\n* `selectFrom` which will be the column we need to display in the option from the relation model.\n* `valueFrom` which will be the column from the relational model that will be the value of the option of the select dropdown.\n\nIf the `type` index in the relation array is `many` you will need to provide \n* `name` index which will be the name of the relation. For example if my model is `User` and it has many `Role` so the name of the relation will be `roles` or whatever the name of the relation you created in the `User` model.\n\nYou will also need to provide a `selectFrom` and `valueFrom` as well.\n\nIf the input is a `select` you can provide a `valueFallback` which will be a static method returns all the options in the dropdown and then you need to provide a `valueCallback` which will return the selected options collection.\n\n##### Additional Preferences\nThere are multiple additional things that can be very usefull in all your fields specifications.\n1. `label_classes` which is the label class attribute value. You can override the default classes which are `control-label col-md-4`.\n2. `input_div_classes` which is the class attribute of the div that contains the input code. By default it's `col-md-6`.\n3. `input_classes`:which is the class attribute of the input itself. By default it's `form-control`.\n4. `input_id` which is the id attribute of the input itself. By default it's the field name.\n5. `inject_attributes` which allows you to add additional attributes to the input. It just takes the string and put it in the input tag.\n\n\n#### Examples.\nA normal text field:\n```PHP\n'name' =\u003e [\n        'label' =\u003e 'Your name',\n        'input' =\u003e 'input',\n        'type' =\u003e 'text'\n    \t],\n```\nA number field:\n```PHP\n'age' =\u003e [\n        'label' =\u003e 'Your age',\n        'input' =\u003e 'input',\n        'type' =\u003e 'number'\n    \t],\n```\nIf the model belongs to a role and we wanna make a select field for the roles.\n```PHP\n'role_id' =\u003e [\n    'label'    =\u003e'Role',\n    'input'    =\u003e 'select',\n    'relation' =\u003e [\n        'model'      =\u003e 'App\\Role',\n        'type'       =\u003e 'one',\n        'column'     =\u003e 'role_id',\n        'selectFrom' =\u003e 'name', \n        'valueFrom'  =\u003e 'id',\n        // This is how selectFrom and valueFrom works\n        // \u003coption value=\"{{ $role-\u003eid }}\"\u003e{{ $role-\u003ename }}\u003c/option\u003e\n    ],\n],\n```\nWhat if the model belongs to many roles?\n```PHP\n'roles[]' =\u003e [\n    'label'    =\u003e 'Roles',\n    'input'    =\u003e 'select',\n    'relation' =\u003e [\n        'name' =\u003e 'roles',\n        'model' =\u003e 'App\\Role',\n        'type' =\u003e 'many',\n        'selectFrom' =\u003e 'name',\n        'valueFrom' =\u003e 'id'\n    ],\n    'inject_attributes' =\u003e 'multiple'\n],\n```\n\nA full example how should be the $fields array of a user model.\n```PHP\n\u003c?php\nnamespace App;\n\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Lilessam\\EloquentFields\\Traits\\FormGenerator;\n\nclass User extends Model\n{\n    use FormGenerator;\n\n    public static $fields = [\n        'name'  =\u003e [\n            'label' =\u003e 'Your name',\n            'input' =\u003e 'input',\n            'type'  =\u003e 'text',\n        ],\n        'age'   =\u003e [\n            'label' =\u003e 'Your age',\n            'input' =\u003e 'input',\n            'type'  =\u003e 'number',\n        ],\n        'roles[]' =\u003e [\n            'label'    =\u003e 'Roles',\n            'input'    =\u003e 'select',\n            'relation' =\u003e [\n                'name'       =\u003e 'roles',\n                'model'      =\u003e 'App\\Role',\n                'type'       =\u003e 'many',\n                'selectFrom' =\u003e 'name',\n                'valueFrom'  =\u003e 'id',\n            ],\n            'inject_attributes' =\u003e 'multiple'\n        ],\n    ];\n\n    public function roles()\n    {\n        return $this-\u003ebelongsToMany('App\\Role');\n    }\n}\n```\n\nExample for valueCallbacks and valueFallbacks.\n```PHP\npublic static $fields = [\n    'exception_ids[]' =\u003e [\n        'label' =\u003e 'المنتجات المستثناة',\n        'input' =\u003e 'select',\n        'valueCallback' =\u003e 'getExceptionsValues',\n        'valueFallback' =\u003e 'getAllProducts',\n        'selectFrom' =\u003e 'name_ar',\n        'valueFrom' =\u003e 'id',\n        'inject_attributes' =\u003e 'multiple'\n    ],\n];\n\npublic function getExceptionsValues()\n{\n    $results = [];\n    $exception_ids = $this-\u003eexception_ids ? json_decode($this-\u003eexception_ids) : [];\n    foreach ($exception_ids as $exception_id) {\n        $product = Product::find($exception_id);\n        $results[] = $product;\n    }\n\n    return $results;\n}\n\npublic static function getAllProducts()\n{\n    return Product::latest()-\u003eget();\n}\n```\n\nExample for manual `belongsTo` relation with createValueCallbacks and updateValueFallbacks.\n```PHP\n/**\n* The attributes that are building the model forms.\n* \n* @var array\n*/\npublic static $fields = [\n    'name_ar' =\u003e [\n        'label' =\u003e 'الاسم بالعربية',\n    ],\n    'name_en' =\u003e [\n        'label' =\u003e 'الاسم بالإنجليزية',\n    ],\n    'category_id' =\u003e [\n        'label' =\u003e 'التصنيف الأساسي',\n        'input' =\u003e 'select',\n        'options' =\u003e [\n            '' =\u003e 'لا تصنيف',\n        ],\n        'selectFrom' =\u003e 'name_ar',\n        'valueFrom' =\u003e 'id',\n        'valueCallback' =\u003e 'getCurrentValue', // Can be replaced with 'column' =\u003e 'category_id' for belongsTo relation\n        'updateValueFallback' =\u003e 'getUpdateCategories',\n        'createValueFallback' =\u003e 'getAllCategories',\n    ],\n];\n\n/**\n * Get dropdown categories for EloquentFields.\n * \n * @return self\n*/\npublic function getUpdateCategories()\n{\n    return static::where('id', '!=', $this-\u003eid)-\u003eget();\n}\n\n/**\n  * Get all dropdown categories for EloquentFields Creation.\n * \n * @return self\n */\npublic static function getAllCategories()\n{\n    return static::latest()-\u003eget();\n}\n\n/**\n * Get Current value for the manual relation.\n * @return mixed\n */\npublic function getCurrentValue()\n{\n    return $this-\u003ecategory_id;\n}\n```\n\nThen you can just call `generate_fields('App\\User')` to generate a creation form fields or `generate_fields('App\\User', 1)` to generate an update form fields for User whose id is 1.\n\nNotice that you can pass a third parameter to `generate_fields` parameter to except one or many fields from being generated in the form.\n\nSo if we called `generate_fields('App\\User', null, 'age')` a creation form will be created without `age` field. You can also pass an array so `generate_fields('App\\User', null, ['age', 'roles'])` will create a creation form without roles and age fields.\n\n##### Don't hesitate to make a pull request or post an issue if found.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flilessam%2Feloquentfields","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flilessam%2Feloquentfields","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flilessam%2Feloquentfields/lists"}