Skip to content

Commit

Permalink
Add dynamic field name for parent-child
Browse files Browse the repository at this point in the history
  • Loading branch information
relliv committed Apr 10, 2023
1 parent 1e2955e commit 397bb82
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class Post extends Model

This trait allows you to generate a slug from a string. When you create a new model, the slug will be generated automatically. If you change the title, the slug will also be updated. See [bootSluggable()](src/Traits/Sluggable.php#L10) method for more details.

> **Note**
> Field name is `parent_id` by default. You can change it in the config file.
> **Note**
> Field names are `slug` and `title` by default. You can change it in the config file.
Expand Down
5 changes: 3 additions & 2 deletions src/Traits/ParentChild.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

namespace LaravelReady\ModelSupport\Traits;

use Illuminate\Support\Facades\Config;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

trait ParentChild
{
public function parent(): BelongsTo
{
return $this->belongsTo(self::class, 'parent_id');
return $this->belongsTo(self::class, Config::get('has_active.parent_id', 'parent_id'));
}

public function children(): HasMany
{
return $this->hasMany(self::class, 'parent_id');
return $this->hasMany(self::class, Config::get('has_active.parent_id', 'parent_id'));
}

public function recursiveParent(): mixed
Expand Down

0 comments on commit 397bb82

Please sign in to comment.