Skip to content

Commit

Permalink
initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
fahedtms committed May 10, 2022
1 parent 3c310ae commit 7e54652
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 9 deletions.
100 changes: 94 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
![Laravel 9.0](https://img.shields.io/badge/Laravel-9.0-f4645f.svg)
![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)


A package to enable assigning extra fields to Eloquent Models

## Contact Me
Expand All @@ -14,13 +13,13 @@ by [Checking my website](https://fahedaljghine.com/).
## Installation

You can install the package via composer:

``` bash
composer require fahedaljghine/laravel-extra-field
```

The package will automatically register itself.


You must publish the migration with:

```bash
Expand Down Expand Up @@ -75,18 +74,107 @@ return [
];
```

## Usage

Add the `HasExtraFields` trait to a model you like to use extras on.

```php
use Fahedaljghine\ModelNote\HasExtraFields;

class YourEloquentModel extends Model
{
use HasExtraFields;
}
```

### Add a new extra field and value

You can add a new extra field like this:

```php
$extra_field = $model->addExtraField('fieldName' , 'fieldType');

//assign value
$extra_field = $model->addExtraValue($extra_field->id , 'filedValue');

//assign value for other instance no need to add extra field again
$extra_field = $otherModel->addExtraValue($extra_field->id , 'filedValue');

//for another instance
$extra_field = $anotherModel->addExtraValue($extra_field->id , 'filedValue');
```

### Add a new extra field string type and assign value

if you want to add a new string extra field for your model you can do it like this:

```php

$model->addStringExtraValue('fieldName' , 'filedValue');

//you can repeat for other object
$otherModel->addExtraValue('fieldName', 'filedValue');

```

### Retrieve data

```php

// will give array of all extra fields with associated values
$model->getExtras(); // ['fieldName1' => 'filedValue' , 'fieldName2' => 'filedValue']


$model->extras(); // will return a collection of Fahedaljghine\ExtraField\Extra


$model->extraValues ; //will return a collection of Fahedaljghine\ExtraField\ExtraValue


$model->extraValues() ; // will return hasMany Relation of Fahedaljghine\ExtraField\ExtraValue
```


### Drop extra field
```php

//this wil drop the value of the given name extra field for this model
$model->dropExtraFieldData($name) ;
```

### Drop extra field and all related data to model
# `use with caution`
```php

//this will drop the extra data field and all associated data with it
$model->dropExtraField($name);
```


### update extra field value
sometimes you may need to update the value of the extra field, you can achive that by using this :
```php

#$extra_field @param String the name of the extra field
#$updated_value @param String the new value

$model->updateExtraValue($extra_field , $updated_value);
```


### Custom models and migrations

You can change the models used by specifying a class name in the `extra_model` & `extra_value_model` key of the `extra-field` config file.
You can change the models used by specifying a class name in the `extra_model` & `extra_value_model` key of
the `extra-field` config file.

You can change the column name used in the extra_values table (`model_id` by default) when using a custom migration where you
You can change the column name used in the extra_values table (`model_id` by default) when using a custom migration
where you
changed that. In that case, simply change the `model_primary_key_attribute` key of the `extra-field` config file.

You can change the column name used in the extra_values table (`model_class` by default) when using a custom migration where you
You can change the column name used in the extra_values table (`model_class` by default) when using a custom migration
where you
changed that. In that case, simply change the `model_name_attribute` key of the `extra-field` config file.


### Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
Expand Down
82 changes: 79 additions & 3 deletions src/HasExtraFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ public function extraValues(): HasMany

public function addExtraField(string $name, string $type)
{
$exist = $this->getExtraModelClassName()::where('name', $name)
$extra_field = $this->getExtraModelClassName()::where('name', $name)
->where('type', $type)
->where('model_class', get_class($this))
->first();

if ($exist)
return $exist;
if ($extra_field)
return $extra_field;

return $this->getExtraModelClassName()::create([
'name' => $name,
Expand All @@ -48,6 +48,81 @@ public function addExtraField(string $name, string $type)
]);
}

public function dropExtraFieldData(string $name): self
{
$extra_field = $this->getExtraModelClassName()::where('name', $name)
->where('model_class', get_class($this))
->first();

if ($extra_field) {
$this->getExtraValueModelClassName()
::where('extra_id', $extra_field->id)
->where('model_id', $this->id)
->delete();

// $extra_field->delete();
}

return $this;
}

public function dropExtraField(string $name): self
{
$extra_field = $this->getExtraModelClassName()::where('name', $name)
->where('model_class', get_class($this))
->first();

if ($extra_field) {
$this->getExtraValueModelClassName()
::where('extra_id', $extra_field->id)
->delete();

$extra_field->delete();
}

return $this;
}

public function updateExtraValue(string $extra_field, string $value): self
{
$extra_field = $this->getExtraModelClassName()::where('name', $extra_field)
->where('model_class', get_class($this))
->first();

if ($extra_field) {
$extra_value = $this->getExtraValueModelClassName()::where('extra_id', $extra_field->id)
->first();
if ($extra_value) {
$extra_value->value = $value;
$extra_value->save();
}
}

return $this;
}

public function addStringExtraValue(string $extra_field, string $value): self
{
$extra_field = $this->getExtraModelClassName()::where('name', $extra_field)
->where('model_class', get_class($this))
->first();

if (is_null($extra_field)) {
$extra_field = $this->getExtraModelClassName()::where('name', $extra_field)
->where('type', "string")
->where('model_class', get_class($this))
->first();
}

$this->getExtraValueModelClassName()::create([
'extra_id' => $extra_field->id,
'model_id' => $this->id,
'value' => $value,
]);

return $this;
}

public function addExtraValue(int $extra_id, string $value): self
{
$this->getExtraValueModelClassName()::create([
Expand All @@ -59,6 +134,7 @@ public function addExtraValue(int $extra_id, string $value): self
return $this;
}


protected function getExtraValueTableName(): string
{
$modelClass = $this->getExtraValueModelClassName();
Expand Down

0 comments on commit 7e54652

Please sign in to comment.