From 70acd47ece0596d9fb72b29beb6eef5609e8dff8 Mon Sep 17 00:00:00 2001 From: Todi Adiyatmo Wijoyo Date: Tue, 22 Mar 2016 06:25:22 +0700 Subject: [PATCH 01/24] add information to retrieve all term --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 58a3fc3..ba7efbf 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,14 @@ $term_benz = Taxonomy::CreateTerm($vocabulary->id, 'Mercedes-Benz', $german_cars $term_ferrari = Taxonomy::CreateTerm($vocabulary->id, 'Ferrari', $italian_cars->id, 0); ``` +Retrieve all term from category +``` +$terms = Taxonomy::getVocabularyByNameAsArray('Cars'); + +// Get a Vocabulary by name as an options array for dropdowns +$terms = Taxonomy::getVocabularyByNameOptionsArray('Cars'); +``` + With the Car Model, I can create a new instance and assign it a term for the make it belongs to: ```php From 31e777d997f329b7be6b86179d8265f46f881ee0 Mon Sep 17 00:00:00 2001 From: Todiadiyatmo Date: Tue, 22 Mar 2016 10:50:15 +0700 Subject: [PATCH 02/24] add description --- README.md | 39 +++++++++++++++++ src/Taxonomy.php | 41 ++++++++++++++---- src/TaxonomyTrait.php | 33 ++++++++++---- ...15_03_21_164940_add_description_column.php | 43 +++++++++++++++++++ 4 files changed, 139 insertions(+), 17 deletions(-) create mode 100644 src/migrations/2015_03_21_164940_add_description_column.php diff --git a/README.md b/README.md index 58a3fc3..cf11d22 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,13 @@ class Car extends \Eloquent { ## Usage +Taxonomy base class +``` +use Devfactory\Taxonomy\Models\Term; +use Devfactory\Taxonomy\Models\TermRelation; +use Devfactory\Taxonomy\Models\Vocabulary; +``` + Creating a vocabulary: ```php @@ -119,10 +126,42 @@ You can also optionally specify a parent term and a weight for each, so you can $german_cars = Taxonomy::createTerm($vocabulary->id, 'German Cars'); $italian_cars = Taxonomy::createTerm($vocabulary->id, 'Italian Cars'); +// Using parent $term_audi = Taxonomy::CreateTerm($vocabulary->id, 'Audi', $german_cars->id, 0); $term_bmw = Taxonomy::CreateTerm($vocabulary->id, 'BMW', $german_cars->id, 1); $term_benz = Taxonomy::CreateTerm($vocabulary->id, 'Mercedes-Benz', $german_cars->id, 2); $term_ferrari = Taxonomy::CreateTerm($vocabulary->id, 'Ferrari', $italian_cars->id, 0); + +// Set Description +Taxonomy::CreateTerm($vocabulary->id, + [ + 'name' => 'Toyota', + 'description'=>'Some description', + 'parent'=>$parent_id, + 'weight'=>$weight, + + ]); + +``` + +Retrieve all term from vocabulary +``` +$terms = Taxonomy::getVocabularyByNameAsArray('Cars'); + +// Get a Vocabulary by name as an options array for dropdowns +$terms = Taxonomy::getVocabularyByNameOptionsArray('Cars'); +``` + +Retrive term from vocabulary +``` +// $term = Taxonomy::getTermByName($vocabulary,$term_name); + +$vocabulary = Taxonomy::getVocabularyByName('Cars'); +$term = Taxonomy::getTermByName($vocabulary,'German Cars'); + +// $term = Taxonomy::getTermByName($vocabulary_id,$term_name); + +$term = Taxonomy::getTermByName($vocabulary->id,'German Cars'); ``` With the Car Model, I can create a new instance and assign it a term for the make it belongs to: diff --git a/src/Taxonomy.php b/src/Taxonomy.php index b699b3b..d964029 100644 --- a/src/Taxonomy.php +++ b/src/Taxonomy.php @@ -30,8 +30,8 @@ public function createVocabulary($name) { throw new Exceptions\VocabularyExistsException(); } - return $this->vocabulary->create(['name' => $name]); - } + return $this->vocabulary->create(['name' => $name]); + } /** * Get a Vocabulary by ID @@ -59,6 +59,24 @@ public function getVocabularyByName($name) { return $this->vocabulary->where('name', $name)->first(); } + /** + * Get a Vocabulary by name + * + * @param string $name + * The name of the Vocabulary to fetch + * + * @return + * The Vocabulary Model object, otherwise NULL + */ + public function getTermByName( $vocabulary, $name ) { + + if( $vocabulary instanceof \Devfactory\Taxonomy\Models\Vocabulary ) + $vocabulary = $vocabulary->id; + + + return $this->term->where( 'vocabulary_id', $vocabulary )->where( 'name', $name )->first(); + } + /** * Get a Vocabulary by name * @@ -185,15 +203,20 @@ public function deleteVocabularyByName($name) { * * @thrown Illuminate\Database\Eloquent\ModelNotFoundException */ - public function createTerm($vid, $name, $parent = 0, $weight = 0) { + public function createTerm($vid, $term, $parent = 0, $weight = 0) { $vocabulary = $this->vocabulary->findOrFail($vid); - $term = [ - 'name' => $name, - 'vocabulary_id' => $vid, - 'parent' => $parent, - 'weight' => $weight, - ]; + if( !is_array($term) ) + { + $term = [ + 'name' => $name, + 'parent' => $parent, + 'weight' => $weight, + ]; + + } + + $term['vocabulary_id'] = $vid; return $this->term->create($term); } diff --git a/src/TaxonomyTrait.php b/src/TaxonomyTrait.php index 3faa802..6b97f55 100644 --- a/src/TaxonomyTrait.php +++ b/src/TaxonomyTrait.php @@ -6,13 +6,13 @@ trait TaxonomyTrait { /** - * Return collection of tags related to the tagged model - * - * @return Illuminate\Database\Eloquent\Collection - */ - public function related() { - return $this->morphMany('Devfactory\Taxonomy\Models\TermRelation', 'relationable'); - } + * Return collection of tags related to the tagged model + * + * @return Illuminate\Database\Eloquent\Collection + */ + public function related() { + return $this->morphMany('Devfactory\Taxonomy\Models\TermRelation', 'relationable'); + } /** * Add an existing term to the inheriting model @@ -23,17 +23,34 @@ public function related() { * @return object * The TermRelation object */ - public function addTerm($term_id) { + public function addTerm($term_id,$description="") { $term = ($term_id instanceof Term) ? $term_id : Term::findOrFail($term_id); $term_relation = [ 'term_id' => $term->id, 'vocabulary_id' => $term->vocabulary_id, + 'description' => $description, ]; $this->related()->save(new TermRelation($term_relation)); } + /** + * Update an existing term to the inheriting model + * + * @param $term_id int + * The ID of the term or an instance of the Term object + * + * @return object + * The TermRelation object + */ + public function updateTerm($term_id,$description="") { + $term = ($term_id instanceof Term) ? $term_id : Term::findOrFail($term_id); + + $this->related()->update(['description' => $description])->where(['term_id' => $term->id]); + } + + /** * Check if the Model instance has the passed term as an existing relation * diff --git a/src/migrations/2015_03_21_164940_add_description_column.php b/src/migrations/2015_03_21_164940_add_description_column.php new file mode 100644 index 0000000..a998e54 --- /dev/null +++ b/src/migrations/2015_03_21_164940_add_description_column.php @@ -0,0 +1,43 @@ +string('description',1000); + }); + + // + Schema::table('term_relations', function ($table) { + $table->string('description',1000); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + Schema::table('terms', function ($table) { + $table->dropColumn('description'); + }); + + // + Schema::table('term_relations', function ($table) { + $table->dropColumn('description'); + }); + } + +} \ No newline at end of file From bbf77d344e392a6d690d6b460a2923a08bbd017f Mon Sep 17 00:00:00 2001 From: Todiadiyatmo Date: Tue, 22 Mar 2016 10:51:51 +0700 Subject: [PATCH 03/24] readme --- README.md | 8 -------- 1 file changed, 8 deletions(-) diff --git a/README.md b/README.md index 189cd81..cf11d22 100644 --- a/README.md +++ b/README.md @@ -164,14 +164,6 @@ $term = Taxonomy::getTermByName($vocabulary,'German Cars'); $term = Taxonomy::getTermByName($vocabulary->id,'German Cars'); ``` -Retrieve all term from category -``` -$terms = Taxonomy::getVocabularyByNameAsArray('Cars'); - -// Get a Vocabulary by name as an options array for dropdowns -$terms = Taxonomy::getVocabularyByNameOptionsArray('Cars'); -``` - With the Car Model, I can create a new instance and assign it a term for the make it belongs to: ```php From 975101abc2fffaa66d7754ef4dbf462e8657b940 Mon Sep 17 00:00:00 2001 From: Todiadiyatmo Date: Tue, 22 Mar 2016 10:54:34 +0700 Subject: [PATCH 04/24] fix --- src/Models/Term.php | 13 +++++++------ src/Models/TermRelation.php | 9 +++++---- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/Models/Term.php b/src/Models/Term.php index b75dd03..dfdfeb5 100755 --- a/src/Models/Term.php +++ b/src/Models/Term.php @@ -7,19 +7,20 @@ class Term extends \Eloquent { 'vocabulary_id', 'parent', 'weight', + 'description' ]; - public static $rules = [ - 'name' => 'required' + public static $rules = [ + 'name' => 'required' ]; public function termRelation() { - return $this->morphMany('Devfactory\Taxonomy\Models\TermRelation', 'relationable'); + return $this->morphMany('TermRelation', 'relationable'); } - public function vocabulary() { - return $this->belongsTo('Devfactory\Taxonomy\Models\Vocabulary'); - } + public function vocabulary() { + return $this->belongsTo('Devfactory\Taxonomy\Models\Vocabulary'); + } public function childrens() { return $this->hasMany('Devfactory\Taxonomy\Models\Term', 'parent', 'id') diff --git a/src/Models/TermRelation.php b/src/Models/TermRelation.php index d42df2a..59ad818 100644 --- a/src/Models/TermRelation.php +++ b/src/Models/TermRelation.php @@ -5,16 +5,17 @@ class TermRelation extends \Eloquent { protected $fillable = [ 'term_id', 'vocabulary_id', + 'description' ]; - protected $table = 'term_relations'; + protected $table = 'term_relations'; public function relationable() { return $this->morphTo(); } - public function term() { - return $this->belongsTo('Devfactory\Taxonomy\Models\Term'); - } + public function term() { + return $this->belongsTo('Devfactory\Taxonomy\Models\Term'); + } } From 8a199e25af8d9317b87d48be293e99d912e05a82 Mon Sep 17 00:00:00 2001 From: Todiadiyatmo Date: Tue, 22 Mar 2016 11:13:04 +0700 Subject: [PATCH 05/24] fix update --- src/TaxonomyTrait.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/TaxonomyTrait.php b/src/TaxonomyTrait.php index 6b97f55..a1307bc 100644 --- a/src/TaxonomyTrait.php +++ b/src/TaxonomyTrait.php @@ -47,7 +47,10 @@ public function addTerm($term_id,$description="") { public function updateTerm($term_id,$description="") { $term = ($term_id instanceof Term) ? $term_id : Term::findOrFail($term_id); - $this->related()->update(['description' => $description])->where(['term_id' => $term->id]); + if( !$this->related() ) + return; + + $this->related()->update(['description' => $description]); } From abc2427d6fd65591a0c2eb21d4bd8c4dd67935cb Mon Sep 17 00:00:00 2001 From: Todiadiyatmo Date: Wed, 23 Mar 2016 07:50:54 +0700 Subject: [PATCH 06/24] fix --- src/Taxonomy.php | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/Taxonomy.php b/src/Taxonomy.php index d964029..c3d41a6 100644 --- a/src/Taxonomy.php +++ b/src/Taxonomy.php @@ -86,8 +86,8 @@ public function getTermByName( $vocabulary, $name ) { * @return * The Vocabulary Model object, otherwise NULL */ - public function getVocabularyByNameAsArray($name) { - $vocabulary = $this->vocabulary->where('name', $name)->first(); + public function getTermsByNameAsArray( $vocabulary_name , $field='name' ) { + $vocabulary = $this->vocabulary->where('name', $vocabulary_name)->first(); if (!is_null($vocabulary)) { return $vocabulary->terms->lists('name', 'id')->toArray(); @@ -96,6 +96,25 @@ public function getVocabularyByNameAsArray($name) { return []; } + /** + * Get a Vocabulary by name + * + * @param string $name + * The name of the Vocabulary to fetch + * + * @return + * The Vocabulary Model object, otherwise NULL + */ + public function getTermsByName( $vocabulary_name ) { + $vocabulary = $this->vocabulary->where('name', $vocabulary_name)->first(); + + if (!is_null($vocabulary)) { + return $vocabulary->terms; + } + + return []; + } + /** * Get a Vocabulary by name as an options array for dropdowns * From 5e0b105e62994c94a1a1cbe6db12bb8611bd6fbd Mon Sep 17 00:00:00 2001 From: Todi Adiyatmo Date: Wed, 23 Mar 2016 18:22:55 +0700 Subject: [PATCH 07/24] add param --- src/Taxonomy.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Taxonomy.php b/src/Taxonomy.php index c3d41a6..ec03580 100644 --- a/src/Taxonomy.php +++ b/src/Taxonomy.php @@ -71,10 +71,22 @@ public function getVocabularyByName($name) { public function getTermByName( $vocabulary, $name ) { if( $vocabulary instanceof \Devfactory\Taxonomy\Models\Vocabulary ) - $vocabulary = $vocabulary->id; + $vocabulary_id = $vocabulary->id; + + if( is_string( $vocabulary ) ) + { + + $vocabulary = $this->getVocabularyByName( $vocabulary ); + + if( !$vocabulary ) + return false; + + $vocabulary_id = $vocabulary->id; + + } - return $this->term->where( 'vocabulary_id', $vocabulary )->where( 'name', $name )->first(); + return $this->term->where( 'vocabulary_id', $vocabulary_id )->where( 'name', $name )->first(); } /** From 072e38ac119cca23fbe60681324e07664426cda4 Mon Sep 17 00:00:00 2001 From: Todiadiyatmo Date: Mon, 28 Mar 2016 00:18:25 +0700 Subject: [PATCH 08/24] add setTerm --- src/Taxonomy.php | 48 +++++++++++++++-------------- src/TaxonomyTrait.php | 70 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 92 insertions(+), 26 deletions(-) diff --git a/src/Taxonomy.php b/src/Taxonomy.php index ec03580..ef47064 100644 --- a/src/Taxonomy.php +++ b/src/Taxonomy.php @@ -9,7 +9,8 @@ class Taxonomy { protected $term; protected $term_relation; - public function __construct(Vocabulary $vocabulary, Term $term) { + public function __construct(Vocabulary $vocabulary, Term $term) + { // Inject required Models $this->vocabulary = $vocabulary; $this->term = $term; @@ -25,8 +26,10 @@ public function __construct(Vocabulary $vocabulary, Term $term) { * The Vocabulary object if created, FALSE if error creating, * Exception if the vocabulary name already exists. */ - public function createVocabulary($name) { - if ($this->vocabulary->where('name', $name)->count()) { + public function createVocabulary($name) + { + if ($this->vocabulary->where('name', $name)->count()) + { throw new Exceptions\VocabularyExistsException(); } @@ -42,7 +45,8 @@ public function createVocabulary($name) { * @return * The Vocabulary Model object, otherwise NULL */ - public function getVocabulary($id) { + public function getVocabulary($id) + { return $this->vocabulary->find($id); } @@ -55,7 +59,8 @@ public function getVocabulary($id) { * @return * The Vocabulary Model object, otherwise NULL */ - public function getVocabularyByName($name) { + public function getVocabularyByName($name) + { return $this->vocabulary->where('name', $name)->first(); } @@ -68,7 +73,8 @@ public function getVocabularyByName($name) { * @return * The Vocabulary Model object, otherwise NULL */ - public function getTermByName( $vocabulary, $name ) { + public function getTermByName( $vocabulary, $name ) + { if( $vocabulary instanceof \Devfactory\Taxonomy\Models\Vocabulary ) $vocabulary_id = $vocabulary->id; @@ -98,7 +104,8 @@ public function getTermByName( $vocabulary, $name ) { * @return * The Vocabulary Model object, otherwise NULL */ - public function getTermsByNameAsArray( $vocabulary_name , $field='name' ) { + public function getTermsByNameAsArray( $vocabulary_name , $field='name' ) + { $vocabulary = $this->vocabulary->where('name', $vocabulary_name)->first(); if (!is_null($vocabulary)) { @@ -117,7 +124,8 @@ public function getTermsByNameAsArray( $vocabulary_name , $field='name' ) { * @return * The Vocabulary Model object, otherwise NULL */ - public function getTermsByName( $vocabulary_name ) { + public function getTermsByName( $vocabulary_name ) + { $vocabulary = $this->vocabulary->where('name', $vocabulary_name)->first(); if (!is_null($vocabulary)) { @@ -136,7 +144,8 @@ public function getTermsByName( $vocabulary_name ) { * @return * The Vocabulary Model object, otherwise NULL */ - public function getVocabularyByNameOptionsArray($name) { + public function getVocabularyByNameOptionsArray($name) + { $vocabulary = $this->vocabulary->where('name', $name)->first(); if (is_null($vocabulary)) { @@ -166,7 +175,8 @@ public function getVocabularyByNameOptionsArray($name) { * * @return array */ - private function recurse_children($parent, &$options, $depth = 1) { + private function recurse_children($parent, &$options, $depth = 1) + { $parent->childrens->map(function($child) use (&$options, $depth) { $options[$child->id] = str_repeat('-', $depth) .' '. $child->name; @@ -204,7 +214,8 @@ public function deleteVocabulary($id) { * * @thrown Illuminate\Database\Eloquent\ModelNotFoundException */ - public function deleteVocabularyByName($name) { + public function deleteVocabularyByName($name) + { $vocabulary = $this->vocabulary->where('name', $name)->first(); if (!is_null($vocabulary)) { @@ -234,20 +245,13 @@ public function deleteVocabularyByName($name) { * * @thrown Illuminate\Database\Eloquent\ModelNotFoundException */ - public function createTerm($vid, $term, $parent = 0, $weight = 0) { + public function createTerm($vid, $term = array() ) + { $vocabulary = $this->vocabulary->findOrFail($vid); - if( !is_array($term) ) - { - $term = [ - 'name' => $name, - 'parent' => $parent, - 'weight' => $weight, - ]; - - } - $term['vocabulary_id'] = $vid; + $term['parent'] = isset($term['parent']) ? $term['parent'] : 0 ; + $term['weight'] = isset($term['weight']) ? $term['weight'] : 0 ; return $this->term->create($term); } diff --git a/src/TaxonomyTrait.php b/src/TaxonomyTrait.php index a1307bc..2bc774b 100644 --- a/src/TaxonomyTrait.php +++ b/src/TaxonomyTrait.php @@ -15,7 +15,7 @@ public function related() { } /** - * Add an existing term to the inheriting model + * Add an existing term to the inheriting model (Many to Many ) * * @param $term_id int * The ID of the term or an instance of the Term object @@ -24,8 +24,12 @@ public function related() { * The TermRelation object */ public function addTerm($term_id,$description="") { + $term = ($term_id instanceof Term) ? $term_id : Term::findOrFail($term_id); + if(!$term) + return; + $term_relation = [ 'term_id' => $term->id, 'vocabulary_id' => $term->vocabulary_id, @@ -35,6 +39,28 @@ public function addTerm($term_id,$description="") { $this->related()->save(new TermRelation($term_relation)); } + /** + * Set term to the inheriting model ( One to many ) + * + * @param $term_id int + * The ID of the term or an instance of the Term object + * + * @return object + * The TermRelation object + */ + public function setTerm($term_id,$description="") { + + $term = ($term_id instanceof Term) ? $term_id : Term::findOrFail($term_id); + + if(!$term) + return; + + // Remove all term from same vocabulary + $this->related()->where('vocabulary_id',$term->vocabulary_id)->delete(); + + $this->addTerm($term_id,$description); + } + /** * Update an existing term to the inheriting model * @@ -47,10 +73,13 @@ public function addTerm($term_id,$description="") { public function updateTerm($term_id,$description="") { $term = ($term_id instanceof Term) ? $term_id : Term::findOrFail($term_id); + if(!$term) + return; + if( !$this->related() ) return; - $this->related()->update(['description' => $description]); + $this->related()->where('term_id',$term_id)->update(['description' => $description]); } @@ -87,7 +116,31 @@ public function hasTerm($term_id) { public function getTermsByVocabularyName($name) { $vocabulary = \Taxonomy::getVocabularyByName($name); - return $this->related()->where('vocabulary_id', $vocabulary->id)->get(); + if(!$vocabulary) + return []; + + return $this->related()->where('term_relations.vocabulary_id', $vocabulary->id)->get(); + } + + /** + * Get all the terms for a given vocabulary that are linked to the current + * Model. + * + * @param $name string + * The name of the vocabulary + * + * @return object + * A collection of TermRelation objects + */ + public function getTermByVocabularyName($name) { + + $vocabulary = \Taxonomy::getVocabularyByName($name); + + if(!$vocabulary) + return []; + + return $this->related()->where('vocabulary_id', $vocabulary->id)->first(); + } /** @@ -133,7 +186,16 @@ public function removeTerm($term_id) { * @return bool * TRUE if the term relation has been deleted, otherwise FALSE */ - public function removeAllTerms() { + public function removeAllTerms($vocabulary_name=false) { + + + if($vocabulary_name) + { + $vocabulary = \Taxonomy::getVocabularyByName($name); + + return $this->related()->where('vocabulary_id',$vocabulary->id)->delete(); + } + return $this->related()->delete(); } From 55f0beb3dd7fef67d7b86dc0aa7338e78a464282 Mon Sep 17 00:00:00 2001 From: Todiadiyatmo Date: Thu, 31 Mar 2016 12:43:31 +0700 Subject: [PATCH 09/24] fix --- README.md | 164 +++++++++++------- src/Controllers/TaxonomyController.php | 10 +- src/Controllers/TermsController.php | 6 +- src/Exceptions/TermExistsException.php | 3 + .../VocabularyNotExistsException.php | 3 + src/Models/Term.php | 6 +- src/Taxonomy.php | 48 +++-- src/TaxonomyTrait.php | 50 +++--- .../2014_10_29_164925_create_terms_table.php | 5 +- 9 files changed, 186 insertions(+), 109 deletions(-) create mode 100644 src/Exceptions/TermExistsException.php create mode 100644 src/Exceptions/VocabularyNotExistsException.php diff --git a/README.md b/README.md index cf11d22..1a11759 100644 --- a/README.md +++ b/README.md @@ -12,10 +12,10 @@ This package allows you to create vocabularies with terms in Laravel 4 and 5 ### Laravel 5 In your `composer.json` add: - - "require": { - "devfactory/taxonomy": "3.0.*" - } +m + "require": { + "devfactory/taxonomy": "3.0.*" + } From the terminal run @@ -31,11 +31,11 @@ Then register the service provider and Facade by opening `app/config/app.php` Then run the following artisant command to publish the config and migrations: - php artisan vendor:publish + php artisan vendor:publish Then run the migrations: - php artisan migrate + php artisan migrate And finally in any of the Models where you want to use the Taxonomy functionality, add the following trait: @@ -51,9 +51,9 @@ class Car extends \Eloquent { In your `composer.json` add: - "require": { - "devfactory/taxonomy": "2.0.*" - } + "require": { + "devfactory/taxonomy": "2.0.*" + } From the terminal run @@ -87,106 +87,140 @@ class Car extends \Eloquent { ## Usage -Taxonomy base class +Use Taxonomy base class ``` use Devfactory\Taxonomy\Models\Term; use Devfactory\Taxonomy\Models\TermRelation; use Devfactory\Taxonomy\Models\Vocabulary; ``` -Creating a vocabulary: +### Taxonomy +`Taxonomy` is the grouping mechanism between model/object. Each group is of `taxonomy` is called `vocabulary` .The name of different group between one `vocabulary` is called `term`. The `term` can have also have parrent-child relationship. + +This goal of this package is to make create and organizing multiple taxonomy as easy as possible + +Sample : + +**Region Vocabulary** + +List of term : + +- Asia + - Indonesia + - Singapore +- Europe + - France + - Germany +- North America + - Canada + - United States of America +- Australia + - Australia + - New Zealand +- Africa + - Egypt + - Marocco + + +#### Vocabulary ```php -Taxonomy::createVocabulary('Cars'); +Taxonomy::createVocabulary('Region'); ``` -Retrieving a Vocabulary: +**Retrieving a vocabulary** ```php $vocabulary = Taxonomy::getVocabulary(1); // Using ID -$vocabulary = Taxonomy::getVocabularyByName('Cars'); // Using Name +$vocabulary = Taxonomy::getVocabularyByName('Region'); // Using Name ``` -Deleting a Vocabulary: +**Deleting a vocabulary** ```php Taxonomy::deleteVocabulary(1); // Using ID -Taxonomy::deleteVocabularyByName('Cars'); // Using Name +Taxonomy::deleteVocabularyByName('Region'); // Using Name ``` -Adding a Term to a vocabulary: - -```php -Taxonomy::createTerm($vocabulary->id, 'Audi'); -``` +#### Term -You can also optionally specify a parent term and a weight for each, so you can group them together and keep them sorted: +**Adding a term to a vocabulary** ```php -$german_cars = Taxonomy::createTerm($vocabulary->id, 'German Cars'); -$italian_cars = Taxonomy::createTerm($vocabulary->id, 'Italian Cars'); +$vocabulary = Taxonomy::getVocabularyByName('Region'); -// Using parent -$term_audi = Taxonomy::CreateTerm($vocabulary->id, 'Audi', $german_cars->id, 0); -$term_bmw = Taxonomy::CreateTerm($vocabulary->id, 'BMW', $german_cars->id, 1); -$term_benz = Taxonomy::CreateTerm($vocabulary->id, 'Mercedes-Benz', $german_cars->id, 2); -$term_ferrari = Taxonomy::CreateTerm($vocabulary->id, 'Ferrari', $italian_cars->id, 0); +$termAsia = Taxonomy::createTerm($vocabulary->id, [ + 'name' => 'Asia', + 'description'=>'Some description ', + 'parent_id'=>0 , // optional param, set 0 if it has not parrent + 'weight'=>0, // optional param, the term can be retrieved later and sort by its weight -// Set Description -Taxonomy::CreateTerm($vocabulary->id, - [ - 'name' => 'Toyota', - 'description'=>'Some description', - 'parent'=>$parent_id, - 'weight'=>$weight, + ]); - ]); +$termIndonesia = Taxonomy::createTerm($vocabulary->id, [ + 'name' => 'Indonesia', + 'description'=>'Some description', + 'parent_id'=>$termAsia->id, + ]); ``` -Retrieve all term from vocabulary + +**Retrive single term** ``` -$terms = Taxonomy::getVocabularyByNameAsArray('Cars'); -// Get a Vocabulary by name as an options array for dropdowns -$terms = Taxonomy::getVocabularyByNameOptionsArray('Cars'); +// Retrive term `Asia` from Vocabulary `Region` +$term = Taxonomy::getTerm('Region', 'Asia') + +// Other method +$vocabularyRegion = Taxonomy::getVocabularyByName('Region'); +$term = Taxonomy::getTerm($vocabularyRegion , 'Asia') + ``` -Retrive term from vocabulary +**Retrive all terms from vocabulary** ``` -// $term = Taxonomy::getTermByName($vocabulary,$term_name); +/* Using Taxonomy Helper*/ -$vocabulary = Taxonomy::getVocabularyByName('Cars'); -$term = Taxonomy::getTermByName($vocabulary,'German Cars'); +// Get term with child +$terms = Taxonomy::getTerms('Asia'); -// $term = Taxonomy::getTermByName($vocabulary_id,$term_name); +// Get term without child +$terms = Taxonomy::getTerms('Asia',true); -$term = Taxonomy::getTermByName($vocabulary->id,'German Cars'); -``` +/*From vocabulary model itself*/ +$vocabularyRegion = Taxonomy::getVocabularyByName('Region'); -With the Car Model, I can create a new instance and assign it a term for the make it belongs to: +// Get term with child +$terms = $vocabularyRegion->terms(); -```php -$car = Car::create([ - 'model' => 'A3', -]); +// Get term without child +$terms = $vocabularyRegion->terms()->where('parent_id',0)->get() -$car->addTerm($term_bmw->id); -$car->addTerm($term_benz->id); -$car->removeAllTerms(); // Remove all terms linked to this car +``` -$car->addTerm($term_ferrari->id); -$car->removeTerm($term_ferrari-id); // Remove a specific term +#### Working with Model -$car->addTerm($term_audi->id); -// Get all the terms from the vocabulary 'Cars' That -// are attached to this Car. -$terms = $car->getTermsByVocabularyName('Cars'); +Make sure your model is already using `\Devfactory\Taxonomy\TaxonomyTrait` + +**Assign one to many relationship** ``` -To retrieve all the cars that match a given term: +$car = \Car::findOrFail(1); -```php -$audis = Car::getAllByTermId($term_audi->id)->get(); +$term = Taxonomy::getTerm('Region', 'Asia'); + +$car->setTerm($term) + +``` + +**Assign many to many relationship** ``` + +$car = \Car::findOrFail(1); + +$car->addTerm(Taxonomy::getTerm('Region', 'Asia')); +$car->addTerm(Taxonomy::getTerm('Region', 'Europe')); + +``` \ No newline at end of file diff --git a/src/Controllers/TaxonomyController.php b/src/Controllers/TaxonomyController.php index 8726da1..a056550 100644 --- a/src/Controllers/TaxonomyController.php +++ b/src/Controllers/TaxonomyController.php @@ -118,18 +118,18 @@ public function getEdit($id) { return Redirect::route($this->route_prefix . 'taxonomy.index'); } - $terms = $vocabulary->terms()->orderBy('parent', 'ASC')->orderBy('weight', 'ASC')->get(); + $terms = $vocabulary->terms()->orderBy('parent_id', 'ASC')->orderBy('weight', 'ASC')->get(); $ordered_terms = []; foreach ($terms as $term) { - if (!$term->parent) { + if (!$term->parent_id) { $ordered_terms[$term->id] = [ 'term' => $term, 'children' => [], ]; } else { - $ordered_terms[$term->parent]['children'][] = $term; + $ordered_terms[$term->parent_id]['children'][] = $term; } } @@ -147,7 +147,7 @@ public function postOrderTerms($id) { foreach ($content as $parent_key => $parent){ $parent_term = Term::find($parent->id); - $parent_term->parent = 0; + $parent_term->parent_id = 0; $parent_term->weight = $parent_key; $parent_term->save(); @@ -158,7 +158,7 @@ public function postOrderTerms($id) { foreach ($parent->children as $child_key => $child){ $child_term = Term::find($child->id); - $child_term->parent = $parent_term->id; + $child_term->parent_id = $parent_term->id; $child_term->weight = $child_key; $child_term->save(); diff --git a/src/Controllers/TermsController.php b/src/Controllers/TermsController.php index dc9bbd2..a1bb994 100644 --- a/src/Controllers/TermsController.php +++ b/src/Controllers/TermsController.php @@ -89,18 +89,18 @@ public function getEdit($id) { public function getIndex() { $vocabulary = Vocabulary::findOrFail(Input::get('id')); - $terms = $vocabulary->terms()->orderBy('parent', 'ASC')->orderBy('weight', 'ASC')->get(); + $terms = $vocabulary->terms()->orderBy('parent_id', 'ASC')->orderBy('weight', 'ASC')->get(); $ordered_terms = []; foreach ($terms as $term) { - if (!$term->parent) { + if (!$term->parent_id) { $ordered_terms[$term->id] = [ 'term' => $term, 'children' => [], ]; } else { - $ordered_terms[$term->parent]['children'][] = $term; + $ordered_terms[$term->parent_id]['children'][] = $term; } } diff --git a/src/Exceptions/TermExistsException.php b/src/Exceptions/TermExistsException.php new file mode 100644 index 0000000..f168497 --- /dev/null +++ b/src/Exceptions/TermExistsException.php @@ -0,0 +1,3 @@ +hasMany('Devfactory\Taxonomy\Models\Term', 'parent', 'id') + return $this->hasMany('Devfactory\Taxonomy\Models\Term', 'parent_id', 'id') ->orderBy('weight', 'ASC'); } public function parentTerm() { - return $this->hasOne('Devfactory\Taxonomy\Models\Term', 'id', 'parent'); + return $this->hasOne('Devfactory\Taxonomy\Models\Term', 'id', 'parent_id'); } } diff --git a/src/Taxonomy.php b/src/Taxonomy.php index ef47064..d691d95 100644 --- a/src/Taxonomy.php +++ b/src/Taxonomy.php @@ -73,7 +73,7 @@ public function getVocabularyByName($name) * @return * The Vocabulary Model object, otherwise NULL */ - public function getTermByName( $vocabulary, $name ) + public function getTerm( $vocabulary, $name ) { if( $vocabulary instanceof \Devfactory\Taxonomy\Models\Vocabulary ) @@ -124,15 +124,25 @@ public function getTermsByNameAsArray( $vocabulary_name , $field='name' ) * @return * The Vocabulary Model object, otherwise NULL */ - public function getTermsByName( $vocabulary_name ) + public function getTerms($vocabulary, $includeChild = true) { - $vocabulary = $this->vocabulary->where('name', $vocabulary_name)->first(); - - if (!is_null($vocabulary)) { + if( $vocabulary instanceof \Devfactory\Taxonomy\Models\Vocabulary ) return $vocabulary->terms; - } - return []; + if( is_string( $vocabulary ) ) + { + + $vocabulary = $this->getVocabularyByName( $vocabulary ); + + if( !$vocabulary ) + return []; + + if($includeChild) + return $vocabulary->terms; + + return $vocabulary->terms()->where($this->term->getTable().'.parent_id',0)->get(); + + } } /** @@ -245,14 +255,30 @@ public function deleteVocabularyByName($name) * * @thrown Illuminate\Database\Eloquent\ModelNotFoundException */ - public function createTerm($vid, $term = array() ) + public function createTerm($vocabulary_name, $term = array() ) { - $vocabulary = $this->vocabulary->findOrFail($vid); + $vocabulary = $this->vocabulary->where('name', $vocabulary_name)->first(); + + if(!$vocabulary) + { + throw new Exceptions\VocabularyNotExistsException(); + } + + + // if($this->vocabulary->terms) - $term['vocabulary_id'] = $vid; - $term['parent'] = isset($term['parent']) ? $term['parent'] : 0 ; + $term['vocabulary_id'] = $vocabulary->id; + $term['parent_id'] = isset($term['parent_id']) ? $term['parent_id'] : 0 ; $term['weight'] = isset($term['weight']) ? $term['weight'] : 0 ; + if($vocabulary->terms() + ->where($this->term->getTable().'.parent_id', $term['parent_id']) + ->where($this->term->getTable().'.name', $term['name']) + ->first()) + { + throw new Exceptions\TermExistsException(); + } + return $this->term->create($term); } diff --git a/src/TaxonomyTrait.php b/src/TaxonomyTrait.php index 2bc774b..5d27712 100644 --- a/src/TaxonomyTrait.php +++ b/src/TaxonomyTrait.php @@ -15,7 +15,7 @@ public function related() { } /** - * Add an existing term to the inheriting model (Many to Many ) + * Set term to the inheriting model ( One to many ) * * @param $term_id int * The ID of the term or an instance of the Term object @@ -23,24 +23,29 @@ public function related() { * @return object * The TermRelation object */ - public function addTerm($term_id,$description="") { - - $term = ($term_id instanceof Term) ? $term_id : Term::findOrFail($term_id); + public function setTerm($term, $description="") { + + $term = ($term instanceof Term) ? $term : Term::findOrFail($term); if(!$term) return; - $term_relation = [ - 'term_id' => $term->id, - 'vocabulary_id' => $term->vocabulary_id, - 'description' => $description, - ]; + // Check if the new term is already there + $related = $this->related()->first(); + + if( $related && $related->term->id == $term->id ) + { + return $this->updateTerm($term, $description); + } + + // Remove all term from same vocabulary + $this->related()->where('relationable_id', $this->id)->delete(); - $this->related()->save(new TermRelation($term_relation)); + return $this->addTerm($term->id,$description); } /** - * Set term to the inheriting model ( One to many ) + * Add an existing term to the inheriting model (Many to Many ) * * @param $term_id int * The ID of the term or an instance of the Term object @@ -48,19 +53,24 @@ public function addTerm($term_id,$description="") { * @return object * The TermRelation object */ - public function setTerm($term_id,$description="") { - - $term = ($term_id instanceof Term) ? $term_id : Term::findOrFail($term_id); + public function addTerm($term, $description="") { + + $term = ($term instanceof Term) ? $term : Term::findOrFail($term); if(!$term) return; - // Remove all term from same vocabulary - $this->related()->where('vocabulary_id',$term->vocabulary_id)->delete(); + $term_relation = [ + 'term_id' => $term->id, + 'vocabulary_id' => $term->vocabulary_id, + 'description' => $description, + ]; - $this->addTerm($term_id,$description); + return $this->related()->save(new TermRelation($term_relation)); } + + /** * Update an existing term to the inheriting model * @@ -70,8 +80,8 @@ public function setTerm($term_id,$description="") { * @return object * The TermRelation object */ - public function updateTerm($term_id,$description="") { - $term = ($term_id instanceof Term) ? $term_id : Term::findOrFail($term_id); + public function updateTerm($term,$description="") { + $term = ($term instanceof Term) ? $term : Term::findOrFail($term); if(!$term) return; @@ -79,7 +89,7 @@ public function updateTerm($term_id,$description="") { if( !$this->related() ) return; - $this->related()->where('term_id',$term_id)->update(['description' => $description]); + $this->related()->where('term_id',$term->id)->update(['description' => $description]); } diff --git a/src/migrations/2014_10_29_164925_create_terms_table.php b/src/migrations/2014_10_29_164925_create_terms_table.php index 9d0d995..fe3078d 100644 --- a/src/migrations/2014_10_29_164925_create_terms_table.php +++ b/src/migrations/2014_10_29_164925_create_terms_table.php @@ -16,8 +16,9 @@ public function up() $table->integer('vocabulary_id')->unsigned(); $table->foreign('vocabulary_id')->references('id')->on('vocabularies')->onDelete('cascade'); $table->string('name'); - $table->integer('parent')->unsigned(); - $table->integer('weight'); + $table->integer('parent_id')->unsigned(); + $table->unique(['parent_id','name']); + $table->integer('weight'); $table->timestamps(); }); } From 4019fb5b14878ae40ce29c56a1e3d2834abbee93 Mon Sep 17 00:00:00 2001 From: Todi Adiyatmo Date: Thu, 31 Mar 2016 21:53:08 +0700 Subject: [PATCH 10/24] fix --- README.md | 106 ++++++++++++++++++++++++++++++++++++++---- src/Taxonomy.php | 30 ++++-------- src/TaxonomyTrait.php | 96 +++++++++++++++++++------------------- 3 files changed, 155 insertions(+), 77 deletions(-) diff --git a/README.md b/README.md index 1a11759..ae3eb88 100644 --- a/README.md +++ b/README.md @@ -138,14 +138,14 @@ $vocabulary = Taxonomy::getVocabularyByName('Region'); // Using Name **Deleting a vocabulary** ```php -Taxonomy::deleteVocabulary(1); // Using ID -Taxonomy::deleteVocabularyByName('Region'); // Using Name + +$vocabulary->delete(); // Using Eloquent delete +Taxonomy::deleteVocabulary('Region'); // Using Name ``` #### Term **Adding a term to a vocabulary** - ```php $vocabulary = Taxonomy::getVocabularyByName('Region'); @@ -165,7 +165,6 @@ $termIndonesia = Taxonomy::createTerm($vocabulary->id, [ ]); ``` - **Retrive single term** ``` @@ -183,10 +182,13 @@ $term = Taxonomy::getTerm($vocabularyRegion , 'Asia') /* Using Taxonomy Helper*/ // Get term with child -$terms = Taxonomy::getTerms('Asia'); +$terms = Taxonomy::getTerms('Region'); -// Get term without child -$terms = Taxonomy::getTerms('Asia',true); +// Get all first level terms ( parent_id = 0 ) +$terms = Taxonomy::getTerms('Region', false); + +// Get terms from Region Vocabulary , child of Asia term +$terms = Taxonomy::getTerms('Region', 'Asia'); /*From vocabulary model itself*/ $vocabularyRegion = Taxonomy::getVocabularyByName('Region'); @@ -201,26 +203,110 @@ $terms = $vocabularyRegion->terms()->where('parent_id',0)->get() #### Working with Model - Make sure your model is already using `\Devfactory\Taxonomy\TaxonomyTrait` **Assign one to many relationship** ``` - $car = \Car::findOrFail(1); $term = Taxonomy::getTerm('Region', 'Asia'); +// term object $car->setTerm($term) +// term id +$car->setTerm(1) + ``` **Assign many to many relationship** ``` - $car = \Car::findOrFail(1); $car->addTerm(Taxonomy::getTerm('Region', 'Asia')); $car->addTerm(Taxonomy::getTerm('Region', 'Europe')); +// by term id +$car->addTerm(1); +``` + +**Check if a model has a term** +``` +$car = \Car::findOrFail(1); + +$term = Taxonomy::getTerm('Region', 'Asia'); + +// by term object +$car->hasTerm($term); + +// by term id +$car->hasTerm(1); +``` + +**Get term(s) from model** +``` +$car = \Car::findOrFail(1); + +// using Vocabulary id +$term = $car->getTerm(1); +$terms = $car->getTerms(1); + +// using Vocabulary Name +$term = $car->getTerm('Asia'); +$terms = $car->getTerms('Asia'); +``` + +**Remove term from model** +``` +$car = \Car::findOrFail(1); + +$term = Taxonomy::getTerm('Region', 'Asia'); + +// Remove using term object +$car->removeTerm($term); + +// Remove using term id +$car->removeTerm(1); + +$car->removeTerms(); +``` + +**Remove all terms from model** +``` +$car = \Car::findOrFail(1); + +// REMOVE ALL TERMS FROM ALL VOCABULARY +$car->removeTerms(); +``` + +**Remove all terms from specific vocabulary from model** +``` +$car = \Car::findOrFail(1); + +// Remove all term with vocabulary id = 1 +$car->removeTerms(1); + +$vocabularyRegion = Taxonomy::getVocabularyByName('Region'); + +$car->removeTerms($vocabularyRegion); +``` + +#### Running Query Againts Model + +**Get all model which belong to certain vocabulary** +``` +$vocabularyRegion = Taxonomy::getVocabularyByName('Region')->list('id'); + +$cars = Car::whereHasVocabulary($vocabularyRegion)->get(); +``` + +**Get all model which belong to certain term(s)** +``` +$terms = Taxonomy::getTerms('Region')->list('id'); + +$cars = Car::whereHasTerm($terms)->get(); + +// Only Child of Asia +$terms = Taxonomy::getTerms('Region','Asia')->list('id'); +$cars = Car::whereHasTerm($terms)->get(); ``` \ No newline at end of file diff --git a/src/Taxonomy.php b/src/Taxonomy.php index d691d95..63048de 100644 --- a/src/Taxonomy.php +++ b/src/Taxonomy.php @@ -3,6 +3,7 @@ use Devfactory\Taxonomy\Models\Vocabulary; use Devfactory\Taxonomy\Models\Term; + class Taxonomy { protected $vocabulary; @@ -207,34 +208,23 @@ private function recurse_children($parent, &$options, $depth = 1) * * @thrown Illuminate\Database\Eloquent\ModelNotFoundException */ - public function deleteVocabulary($id) { - $vocabulary = $this->vocabulary->findOrFail($id); + public function deleteVocabulary($vocabulary) { - return $vocabulary->delete(); - } + if( is_string( $vocabulary ) ) + { - /** - * Delete a Vocabulary by ID - * - * @param int $id - * The ID of the Vocabulary to delete - * - * @return bool - * TRUE if Vocabulary is deletes, otherwise FALSE - * - * @thrown Illuminate\Database\Eloquent\ModelNotFoundException - */ - public function deleteVocabularyByName($name) - { - $vocabulary = $this->vocabulary->where('name', $name)->first(); + $vocabulary = $this->getVocabularyByName( $vocabulary ); - if (!is_null($vocabulary)) { + if( !$vocabulary ) + return false; + return $vocabulary->delete(); } - return FALSE; + return false; } + /** * Create a new term in a specific vocabulary * diff --git a/src/TaxonomyTrait.php b/src/TaxonomyTrait.php index 5d27712..a5bed00 100644 --- a/src/TaxonomyTrait.php +++ b/src/TaxonomyTrait.php @@ -2,6 +2,8 @@ use Devfactory\Taxonomy\Models\TermRelation; use Devfactory\Taxonomy\Models\Term; +use Devfactory\Taxonomy\Models\Vocabulary; +use Devfactory\Taxonomy; trait TaxonomyTrait { @@ -102,15 +104,15 @@ public function updateTerm($term,$description="") { * @return object * The TermRelation object */ - public function hasTerm($term_id) { - $term = ($term_id instanceof Term) ? $term_id : Term::findOrFail($term_id); + public function hasTerm($term) { - $term_relation = [ - 'term_id' => $term->id, - 'vocabulary_id' => $term->vocabulary_id, - ]; + $term = ($term instanceof Term) ? $term : Term::findOrFail($term); + + if(!$term) + return false; + } - return ($this->related()->where('term_id', $term_id)->count()) ? TRUE : FALSE; + return ($this->related()->where('term_id', $term->id )->count()) ? TRUE : FALSE; } /** @@ -123,11 +125,12 @@ public function hasTerm($term_id) { * @return object * A collection of TermRelation objects */ - public function getTermsByVocabularyName($name) { - $vocabulary = \Taxonomy::getVocabularyByName($name); + public function getTerms($vocabulary) { + + $vocabulary = ($vocabulary instanceof Vocabulary) ? $vocabulary : Vocabulary::findOrFail($vocabulary); if(!$vocabulary) - return []; + return; return $this->related()->where('term_relations.vocabulary_id', $vocabulary->id)->get(); } @@ -142,39 +145,16 @@ public function getTermsByVocabularyName($name) { * @return object * A collection of TermRelation objects */ - public function getTermByVocabularyName($name) { + public function getTerm($name) { - $vocabulary = \Taxonomy::getVocabularyByName($name); + $vocabulary = ($vocabulary instanceof Vocabulary) ? $vocabulary : Vocabulary::findOrFail($vocabulary); if(!$vocabulary) - return []; - - return $this->related()->where('vocabulary_id', $vocabulary->id)->first(); + return; + return $this->related()->where('term_relations.vocabulary_id', $vocabulary->id)->first(); } - /** - * Get all the terms for a given vocabulary that are linked to the current - * Model as a key value pair array. - * - * @param $name string - * The name of the vocabulary - * - * @return array - * A key value pair array of the type 'id' => 'name' - */ - public function getTermsByVocabularyNameAsArray($name) { - $vocabulary = \Taxonomy::getVocabularyByName($name); - - $term_relations = $this->related()->where('vocabulary_id', $vocabulary->id)->get(); - - $data = []; - foreach ($term_relations as $term_relation) { - $data[$term_relation->term->id] = $term_relation->term->name; - } - - return $data; - } /** * Unlink the given term with the current model object @@ -185,8 +165,9 @@ public function getTermsByVocabularyNameAsArray($name) { * @return bool * TRUE if the term relation has been deleted, otherwise FALSE */ - public function removeTerm($term_id) { - $term_id = ($term_id instanceof Term) ? $term_id->id : $term_id; + public function removeTerm($term) { + $term_id = ($term instanceof Term) ? $term->id : $term; + return $this->related()->where('term_id', $term_id)->delete(); } @@ -196,17 +177,19 @@ public function removeTerm($term_id) { * @return bool * TRUE if the term relation has been deleted, otherwise FALSE */ - public function removeAllTerms($vocabulary_name=false) { + public function removeTerms($vocabulary=false) { + if!($vocabulary) + return $this->related()->delete(); - if($vocabulary_name) - { - $vocabulary = \Taxonomy::getVocabularyByName($name); - - return $this->related()->where('vocabulary_id',$vocabulary->id)->delete(); + $vocabulary = ($vocabulary instanceof Vocabulary) ? $vocabulary : Vocabulary::findOrFail($vocabulary); + + if(!$vocabulary) + return false; } - return $this->related()->delete(); + return $this->related()->where('vocabulary_id', $vocabulary->id )->delete(); + } /** @@ -217,7 +200,26 @@ public function removeAllTerms($vocabulary_name=false) { * * @return void */ - public function scopeGetAllByTermId($query, $term_id) { + public function scopeWhereHasTerm($query, $term_id) { + return $query->whereHas('related', function($q) use($term_id) { + if (is_array($term_id)) { + $q->whereIn('term_id', $term_id); + } + else { + $q->where('term_id', '=', $term_id); + } + }); + } + + /** + * Filter the model to return a subset of entries matching the term ID + * + * @param object $query + * @param int $term_id + * + * @return void + */ + public function scopeWhereHasVocabulary($query, $term_id) { return $query->whereHas('related', function($q) use($term_id) { if (is_array($term_id)) { $q->whereIn('term_id', $term_id); From 87a50c9c1022b1f20c07de7bc40ff05488bf58e4 Mon Sep 17 00:00:00 2001 From: Todi Adiyatmo Wijoyo Date: Thu, 31 Mar 2016 21:56:31 +0700 Subject: [PATCH 11/24] Update README.md --- README.md | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index ae3eb88..cfe392e 100644 --- a/README.md +++ b/README.md @@ -124,21 +124,19 @@ List of term : #### Vocabulary +**Create vocabulary** ```php Taxonomy::createVocabulary('Region'); ``` -**Retrieving a vocabulary** - +**Retrieve vocabulary** ```php $vocabulary = Taxonomy::getVocabulary(1); // Using ID $vocabulary = Taxonomy::getVocabularyByName('Region'); // Using Name ``` -**Deleting a vocabulary** - +**Delete a vocabulary** ```php - $vocabulary->delete(); // Using Eloquent delete Taxonomy::deleteVocabulary('Region'); // Using Name ``` @@ -166,19 +164,17 @@ $termIndonesia = Taxonomy::createTerm($vocabulary->id, [ ``` **Retrive single term** -``` - +```php // Retrive term `Asia` from Vocabulary `Region` $term = Taxonomy::getTerm('Region', 'Asia') // Other method $vocabularyRegion = Taxonomy::getVocabularyByName('Region'); $term = Taxonomy::getTerm($vocabularyRegion , 'Asia') - ``` **Retrive all terms from vocabulary** -``` +```php /* Using Taxonomy Helper*/ // Get term with child @@ -198,7 +194,6 @@ $terms = $vocabularyRegion->terms(); // Get term without child $terms = $vocabularyRegion->terms()->where('parent_id',0)->get() - ``` #### Working with Model @@ -206,7 +201,7 @@ $terms = $vocabularyRegion->terms()->where('parent_id',0)->get() Make sure your model is already using `\Devfactory\Taxonomy\TaxonomyTrait` **Assign one to many relationship** -``` +```php $car = \Car::findOrFail(1); $term = Taxonomy::getTerm('Region', 'Asia'); @@ -220,7 +215,7 @@ $car->setTerm(1) ``` **Assign many to many relationship** -``` +```php $car = \Car::findOrFail(1); $car->addTerm(Taxonomy::getTerm('Region', 'Asia')); @@ -231,7 +226,7 @@ $car->addTerm(1); ``` **Check if a model has a term** -``` +```php $car = \Car::findOrFail(1); $term = Taxonomy::getTerm('Region', 'Asia'); @@ -244,7 +239,7 @@ $car->hasTerm(1); ``` **Get term(s) from model** -``` +```php $car = \Car::findOrFail(1); // using Vocabulary id @@ -257,7 +252,7 @@ $terms = $car->getTerms('Asia'); ``` **Remove term from model** -``` +```php $car = \Car::findOrFail(1); $term = Taxonomy::getTerm('Region', 'Asia'); @@ -272,7 +267,7 @@ $car->removeTerms(); ``` **Remove all terms from model** -``` +```php $car = \Car::findOrFail(1); // REMOVE ALL TERMS FROM ALL VOCABULARY @@ -280,7 +275,7 @@ $car->removeTerms(); ``` **Remove all terms from specific vocabulary from model** -``` +```php $car = \Car::findOrFail(1); // Remove all term with vocabulary id = 1 @@ -291,17 +286,17 @@ $vocabularyRegion = Taxonomy::getVocabularyByName('Region'); $car->removeTerms($vocabularyRegion); ``` -#### Running Query Againts Model +#### Running Query against Model **Get all model which belong to certain vocabulary** -``` +```php $vocabularyRegion = Taxonomy::getVocabularyByName('Region')->list('id'); $cars = Car::whereHasVocabulary($vocabularyRegion)->get(); ``` **Get all model which belong to certain term(s)** -``` +```php $terms = Taxonomy::getTerms('Region')->list('id'); $cars = Car::whereHasTerm($terms)->get(); @@ -309,4 +304,4 @@ $cars = Car::whereHasTerm($terms)->get(); // Only Child of Asia $terms = Taxonomy::getTerms('Region','Asia')->list('id'); $cars = Car::whereHasTerm($terms)->get(); -``` \ No newline at end of file +``` From 9a7d7e55c87e82c473356487f59e7ce59abb719d Mon Sep 17 00:00:00 2001 From: Todiadiyatmo Date: Fri, 1 Apr 2016 14:14:26 +0700 Subject: [PATCH 12/24] readme --- README.md | 10 +++++----- src/Taxonomy.php | 28 +++++++++++++++++----------- src/TaxonomyTrait.php | 15 ++++++--------- 3 files changed, 28 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index cfe392e..47444a2 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[![Build Status](https://travis-ci.org/DevFactoryCH/taxonomy.svg?branch=master)](https://travis-ci.org/DevFactoryCH/taxonomy) +r[![Build Status](https://travis-ci.org/DevFactoryCH/taxonomy.svg?branch=master)](https://travis-ci.org/DevFactoryCH/taxonomy) [![Latest Stable Version](https://poser.pugx.org/devfactory/taxonomy/v/stable.svg)](https://packagist.org/packages/devfactory/taxonomy) [![Total Downloads](https://poser.pugx.org/devfactory/taxonomy/downloads.svg)](https://packagist.org/packages/devfactory/taxonomy) [![License](https://poser.pugx.org/devfactory/taxonomy/license.svg)](https://packagist.org/packages/devfactory/taxonomy) @@ -290,18 +290,18 @@ $car->removeTerms($vocabularyRegion); **Get all model which belong to certain vocabulary** ```php -$vocabularyRegion = Taxonomy::getVocabularyByName('Region')->list('id'); +$vocabularyRegion = Taxonomy::getVocabularyByName('Region'); -$cars = Car::whereHasVocabulary($vocabularyRegion)->get(); +$cars = Car::whereHasVocabulary($vocabularyRegion->id)->get(); ``` **Get all model which belong to certain term(s)** ```php -$terms = Taxonomy::getTerms('Region')->list('id'); +$terms = Taxonomy::getTerms('Region')->pluck('id'); $cars = Car::whereHasTerm($terms)->get(); // Only Child of Asia -$terms = Taxonomy::getTerms('Region','Asia')->list('id'); +$terms = Taxonomy::getTerms('Region','Asia')->pluck('id'); $cars = Car::whereHasTerm($terms)->get(); ``` diff --git a/src/Taxonomy.php b/src/Taxonomy.php index 63048de..6365385 100644 --- a/src/Taxonomy.php +++ b/src/Taxonomy.php @@ -125,25 +125,31 @@ public function getTermsByNameAsArray( $vocabulary_name , $field='name' ) * @return * The Vocabulary Model object, otherwise NULL */ - public function getTerms($vocabulary, $includeChild = true) + public function getTerms($vocabulary, $parentName = true, $withParent = false) { if( $vocabulary instanceof \Devfactory\Taxonomy\Models\Vocabulary ) + $vocabulary = $vocabulary; + elseif( is_string( $vocabulary ) ) + $vocabulary = $this->getVocabularyByName( $vocabulary ); + else + return collect([]); + + if( $parentName === true ) return $vocabulary->terms; - if( is_string( $vocabulary ) ) + if( is_string($parentName) ) { + $term = $this->getTerm($vocabulary, $parentName); - $vocabulary = $this->getVocabularyByName( $vocabulary ); - - if( !$vocabulary ) - return []; + $terms = $vocabulary->terms()->where($this->term->getTable().'.parent_id', $term->id)->get(); - if($includeChild) - return $vocabulary->terms; - - return $vocabulary->terms()->where($this->term->getTable().'.parent_id',0)->get(); + if( $withParent ) + return $terms->push($term); + return $terms; } + + return collect([]); } /** @@ -160,7 +166,7 @@ public function getVocabularyByNameOptionsArray($name) $vocabulary = $this->vocabulary->where('name', $name)->first(); if (is_null($vocabulary)) { - return []; + return collect([]); } $parents = $this->term->whereParent(0) diff --git a/src/TaxonomyTrait.php b/src/TaxonomyTrait.php index a5bed00..4e0d5dd 100644 --- a/src/TaxonomyTrait.php +++ b/src/TaxonomyTrait.php @@ -3,7 +3,6 @@ use Devfactory\Taxonomy\Models\TermRelation; use Devfactory\Taxonomy\Models\Term; use Devfactory\Taxonomy\Models\Vocabulary; -use Devfactory\Taxonomy; trait TaxonomyTrait { @@ -110,7 +109,6 @@ public function hasTerm($term) { if(!$term) return false; - } return ($this->related()->where('term_id', $term->id )->count()) ? TRUE : FALSE; } @@ -179,14 +177,13 @@ public function removeTerm($term) { */ public function removeTerms($vocabulary=false) { - if!($vocabulary) + if($vocabulary) return $this->related()->delete(); $vocabulary = ($vocabulary instanceof Vocabulary) ? $vocabulary : Vocabulary::findOrFail($vocabulary); if(!$vocabulary) return false; - } return $this->related()->where('vocabulary_id', $vocabulary->id )->delete(); @@ -219,13 +216,13 @@ public function scopeWhereHasTerm($query, $term_id) { * * @return void */ - public function scopeWhereHasVocabulary($query, $term_id) { - return $query->whereHas('related', function($q) use($term_id) { - if (is_array($term_id)) { - $q->whereIn('term_id', $term_id); + public function scopeWhereHasVocabulary($query, $vocabulary_id) { + return $query->whereHas('related', function($q) use($vocabulary_id) { + if (is_array($vocabulary_id)) { + $q->whereIn('vocabulary_id', $vocabulary_id); } else { - $q->where('term_id', '=', $term_id); + $q->where('vocabulary_id', '=', $vocabulary_id); } }); } From b7cce27096bc71a0d549adba4a9356c3045b4da3 Mon Sep 17 00:00:00 2001 From: Todiadiyatmo Date: Fri, 1 Apr 2016 21:12:43 +0700 Subject: [PATCH 13/24] alpha release --- README.md | 17 +++++++++---- src/Taxonomy.php | 57 +++++++++++++------------------------------ src/TaxonomyTrait.php | 27 ++++++++++++-------- 3 files changed, 46 insertions(+), 55 deletions(-) diff --git a/README.md b/README.md index 47444a2..938ec41 100644 --- a/README.md +++ b/README.md @@ -239,16 +239,23 @@ $car->hasTerm(1); ``` **Get term(s) from model** + +`getTerm` and `getTerms` will return `TermRelation` Model + ```php $car = \Car::findOrFail(1); // using Vocabulary id -$term = $car->getTerm(1); -$terms = $car->getTerms(1); +$termRelation = $car->getTerm(1); +$termRelations = $car->getTerms(1); // using Vocabulary Name -$term = $car->getTerm('Asia'); -$terms = $car->getTerms('Asia'); +$termRelation = $car->getTerm('Region'); +$termRelations = $car->getTerms('Region'); + +$term = $termRelation->term; +$terms = $termRelations->term; + ``` **Remove term from model** @@ -290,7 +297,7 @@ $car->removeTerms($vocabularyRegion); **Get all model which belong to certain vocabulary** ```php -$vocabularyRegion = Taxonomy::getVocabularyByName('Region'); +$vocabularyRegion = Taxonomy::getVocabulary('Region'); $cars = Car::whereHasVocabulary($vocabularyRegion->id)->get(); ``` diff --git a/src/Taxonomy.php b/src/Taxonomy.php index 6365385..4d0b2f3 100644 --- a/src/Taxonomy.php +++ b/src/Taxonomy.php @@ -46,23 +46,16 @@ public function createVocabulary($name) * @return * The Vocabulary Model object, otherwise NULL */ - public function getVocabulary($id) + public function getVocabulary($vocabulary) { - return $this->vocabulary->find($id); - } - - /** - * Get a Vocabulary by name - * - * @param string $name - * The name of the Vocabulary to fetch - * - * @return - * The Vocabulary Model object, otherwise NULL - */ - public function getVocabularyByName($name) - { - return $this->vocabulary->where('name', $name)->first(); + if($vocabulary instanceof Vocabulary) + return $vocabulary; + elseif(is_string($vocabulary)) + return $this->vocabulary->where('name', $vocabulary)->first(); + elseif(is_numeric($vocabulary)) + return $this->vocabulary->where('id', $vocabulary)->first(); + + return false; } /** @@ -77,23 +70,9 @@ public function getVocabularyByName($name) public function getTerm( $vocabulary, $name ) { - if( $vocabulary instanceof \Devfactory\Taxonomy\Models\Vocabulary ) - $vocabulary_id = $vocabulary->id; + $vocabulary = $this->getVocabulary($vocabulary); - if( is_string( $vocabulary ) ) - { - - $vocabulary = $this->getVocabularyByName( $vocabulary ); - - if( !$vocabulary ) - return false; - - $vocabulary_id = $vocabulary->id; - - } - - - return $this->term->where( 'vocabulary_id', $vocabulary_id )->where( 'name', $name )->first(); + return $this->term->where( 'vocabulary_id', $vocabulary->id )->where( 'name', $name )->first(); } /** @@ -105,9 +84,9 @@ public function getTerm( $vocabulary, $name ) * @return * The Vocabulary Model object, otherwise NULL */ - public function getTermsByNameAsArray( $vocabulary_name , $field='name' ) + public function getTermsByNameAsArray( $vocabulary , $field='name' ) { - $vocabulary = $this->vocabulary->where('name', $vocabulary_name)->first(); + $vocabulary = $this->getVocabulary($vocabulary); if (!is_null($vocabulary)) { return $vocabulary->terms->lists('name', 'id')->toArray(); @@ -127,11 +106,9 @@ public function getTermsByNameAsArray( $vocabulary_name , $field='name' ) */ public function getTerms($vocabulary, $parentName = true, $withParent = false) { - if( $vocabulary instanceof \Devfactory\Taxonomy\Models\Vocabulary ) - $vocabulary = $vocabulary; - elseif( is_string( $vocabulary ) ) - $vocabulary = $this->getVocabularyByName( $vocabulary ); - else + $vocabulary = $this->getVocabulary($vocabulary); + + if(!$vocabulary) return collect([]); if( $parentName === true ) @@ -219,7 +196,7 @@ public function deleteVocabulary($vocabulary) { if( is_string( $vocabulary ) ) { - $vocabulary = $this->getVocabularyByName( $vocabulary ); + $vocabulary = $this->getVocabulary( $vocabulary ); if( !$vocabulary ) return false; diff --git a/src/TaxonomyTrait.php b/src/TaxonomyTrait.php index 4e0d5dd..e9489be 100644 --- a/src/TaxonomyTrait.php +++ b/src/TaxonomyTrait.php @@ -3,6 +3,7 @@ use Devfactory\Taxonomy\Models\TermRelation; use Devfactory\Taxonomy\Models\Term; use Devfactory\Taxonomy\Models\Vocabulary; +use Devfactory\Taxonomy\Facades\TaxonomyFacade as Taxonomy; trait TaxonomyTrait { @@ -125,7 +126,7 @@ public function hasTerm($term) { */ public function getTerms($vocabulary) { - $vocabulary = ($vocabulary instanceof Vocabulary) ? $vocabulary : Vocabulary::findOrFail($vocabulary); + $vocabulary = Taxonomy::getVocabulary($vocabulary); if(!$vocabulary) return; @@ -143,12 +144,9 @@ public function getTerms($vocabulary) { * @return object * A collection of TermRelation objects */ - public function getTerm($name) { + public function getTerm($vocabulary) { - $vocabulary = ($vocabulary instanceof Vocabulary) ? $vocabulary : Vocabulary::findOrFail($vocabulary); - - if(!$vocabulary) - return; + $vocabulary = Taxonomy::getVocabulary($vocabulary); return $this->related()->where('term_relations.vocabulary_id', $vocabulary->id)->first(); } @@ -177,10 +175,7 @@ public function removeTerm($term) { */ public function removeTerms($vocabulary=false) { - if($vocabulary) - return $this->related()->delete(); - - $vocabulary = ($vocabulary instanceof Vocabulary) ? $vocabulary : Vocabulary::findOrFail($vocabulary); + $vocabulary = Taxonomy::getVocabulary($vocabulary); if(!$vocabulary) return false; @@ -199,7 +194,14 @@ public function removeTerms($vocabulary=false) { */ public function scopeWhereHasTerm($query, $term_id) { return $query->whereHas('related', function($q) use($term_id) { + + if( method_exists($term_id,'toArray') ) + $term_id = $term_id->toArray(); + if (is_array($term_id)) { + + + $q->whereIn('term_id', $term_id); } else { @@ -218,7 +220,12 @@ public function scopeWhereHasTerm($query, $term_id) { */ public function scopeWhereHasVocabulary($query, $vocabulary_id) { return $query->whereHas('related', function($q) use($vocabulary_id) { + + if( method_exists($vocabulary_id,'toArray') ) + $vocabulary_id = $vocabulary_id->toArray(); + if (is_array($vocabulary_id)) { + $q->whereIn('vocabulary_id', $vocabulary_id); } else { From 79ea409fe6757e8c70b6360db14c874c8d9fabeb Mon Sep 17 00:00:00 2001 From: Todiadiyatmo Date: Fri, 1 Apr 2016 23:45:53 +0700 Subject: [PATCH 14/24] add_term set_term fix --- src/Models/Term.php | 6 +++--- src/Taxonomy.php | 9 +++++---- src/TaxonomyTrait.php | 27 ++++++++++++++++----------- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/Models/Term.php b/src/Models/Term.php index 86399fc..5aa035a 100755 --- a/src/Models/Term.php +++ b/src/Models/Term.php @@ -23,11 +23,11 @@ public function vocabulary() { } public function childrens() { - return $this->hasMany('Devfactory\Taxonomy\Models\Term', 'parent_id', 'id') - ->orderBy('weight', 'ASC'); + return $this->hasMany('Devfactory\Taxonomy\Models\Term', 'parent_id', 'id'); } - public function parentTerm() { + public function parent() { return $this->hasOne('Devfactory\Taxonomy\Models\Term', 'id', 'parent_id'); } + } diff --git a/src/Taxonomy.php b/src/Taxonomy.php index 4d0b2f3..ad1eecd 100644 --- a/src/Taxonomy.php +++ b/src/Taxonomy.php @@ -114,11 +114,12 @@ public function getTerms($vocabulary, $parentName = true, $withParent = false) if( $parentName === true ) return $vocabulary->terms; - if( is_string($parentName) ) + + if( $parentName ) { $term = $this->getTerm($vocabulary, $parentName); - $terms = $vocabulary->terms()->where($this->term->getTable().'.parent_id', $term->id)->get(); + $terms = $vocabulary->terms()->where($this->term->getTable().'.parent_id','=', $term->id)->get(); if( $withParent ) return $terms->push($term); @@ -228,9 +229,9 @@ public function deleteVocabulary($vocabulary) { * * @thrown Illuminate\Database\Eloquent\ModelNotFoundException */ - public function createTerm($vocabulary_name, $term = array() ) + public function createTerm($vocabulary, $term = array() ) { - $vocabulary = $this->vocabulary->where('name', $vocabulary_name)->first(); + $vocabulary = $this->getVocabulary($vocabulary); if(!$vocabulary) { diff --git a/src/TaxonomyTrait.php b/src/TaxonomyTrait.php index e9489be..684aa2c 100644 --- a/src/TaxonomyTrait.php +++ b/src/TaxonomyTrait.php @@ -3,7 +3,6 @@ use Devfactory\Taxonomy\Models\TermRelation; use Devfactory\Taxonomy\Models\Term; use Devfactory\Taxonomy\Models\Vocabulary; -use Devfactory\Taxonomy\Facades\TaxonomyFacade as Taxonomy; trait TaxonomyTrait { @@ -32,18 +31,21 @@ public function setTerm($term, $description="") { if(!$term) return; + if( $this->hasTerm($term) ) + return $this->updateTerm($term,$description); + // Check if the new term is already there - $related = $this->related()->first(); + // $related = $this->related()->first(); - if( $related && $related->term->id == $term->id ) - { - return $this->updateTerm($term, $description); - } + // if( $related && $related->term->id == $term->id ) + // { + // return $this->updateTerm($term, $description); + // } // Remove all term from same vocabulary - $this->related()->where('relationable_id', $this->id)->delete(); + $this->related()->where('relationable_id', $this->id)->where('vocabulary_id',$term->vocabulary->id)->delete(); - return $this->addTerm($term->id,$description); + return $this->addTerm($term,$description); } /** @@ -62,6 +64,9 @@ public function addTerm($term, $description="") { if(!$term) return; + if( $this->hasTerm($term) ) + return $this->updateTerm($term,$description); + $term_relation = [ 'term_id' => $term->id, 'vocabulary_id' => $term->vocabulary_id, @@ -126,7 +131,7 @@ public function hasTerm($term) { */ public function getTerms($vocabulary) { - $vocabulary = Taxonomy::getVocabulary($vocabulary); + $vocabulary = \Devfactory\Taxonomy\Facades\TaxonomyFacade::getVocabulary($vocabulary); if(!$vocabulary) return; @@ -146,7 +151,7 @@ public function getTerms($vocabulary) { */ public function getTerm($vocabulary) { - $vocabulary = Taxonomy::getVocabulary($vocabulary); + $vocabulary = \Devfactory\Taxonomy\Facades\TaxonomyFacade::getVocabulary($vocabulary); return $this->related()->where('term_relations.vocabulary_id', $vocabulary->id)->first(); } @@ -175,7 +180,7 @@ public function removeTerm($term) { */ public function removeTerms($vocabulary=false) { - $vocabulary = Taxonomy::getVocabulary($vocabulary); + $vocabulary = \Devfactory\Taxonomy\Facades\TaxonomyFacade::getVocabulary($vocabulary); if(!$vocabulary) return false; From fe1526db5075b06d0b7333fbc619c2c6bb4dc957 Mon Sep 17 00:00:00 2001 From: Todiadiyatmo Date: Sat, 2 Apr 2016 00:51:53 +0700 Subject: [PATCH 15/24] alpha 1 --- src/Models/TermRelation.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Models/TermRelation.php b/src/Models/TermRelation.php index 59ad818..ea12b34 100644 --- a/src/Models/TermRelation.php +++ b/src/Models/TermRelation.php @@ -14,6 +14,11 @@ public function relationable() { return $this->morphTo(); } + public function vocabulary() { + return $this->belongsTo('Devfactory\Taxonomy\Models\Vocabulary'); + } + + public function term() { return $this->belongsTo('Devfactory\Taxonomy\Models\Term'); } From 089e979ebdf33340227ce04dc41e82842875fa8c Mon Sep 17 00:00:00 2001 From: Todiadiyatmo Date: Mon, 4 Apr 2016 12:55:35 +0700 Subject: [PATCH 16/24] add taxonomy field in json return --- src/Models/Term.php | 10 ++++++++ src/Models/TermRelation.php | 3 ++- src/Models/Vocabulary.php | 2 +- src/Taxonomy.php | 16 +++++++++++++ src/TaxonomyTrait.php | 48 +++++++++++++++++++++++++++++++++++++ 5 files changed, 77 insertions(+), 2 deletions(-) diff --git a/src/Models/Term.php b/src/Models/Term.php index 5aa035a..4a9972f 100755 --- a/src/Models/Term.php +++ b/src/Models/Term.php @@ -2,6 +2,8 @@ class Term extends \Eloquent { + protected $hidden = ['created_at','updated_at']; + protected $fillable = [ 'name', 'vocabulary_id', @@ -14,6 +16,8 @@ class Term extends \Eloquent { 'name' => 'required' ]; + + public function termRelation() { return $this->morphMany('TermRelation', 'relationable'); } @@ -30,4 +34,10 @@ public function parent() { return $this->hasOne('Devfactory\Taxonomy\Models\Term', 'id', 'parent_id'); } + public function root() + { + return \Devfactory\Taxonomy\Facades\TaxonomyFacade::recurseRoot($this); + } + + } diff --git a/src/Models/TermRelation.php b/src/Models/TermRelation.php index ea12b34..44b0a95 100644 --- a/src/Models/TermRelation.php +++ b/src/Models/TermRelation.php @@ -1,7 +1,8 @@ parent_id == 0 ) + return $term; + + if($term->parent) + { + return $this->recurseRoot($term->parent); + } + + } + + + /** * Delete a Vocabulary by ID * diff --git a/src/TaxonomyTrait.php b/src/TaxonomyTrait.php index 684aa2c..6e38a4d 100644 --- a/src/TaxonomyTrait.php +++ b/src/TaxonomyTrait.php @@ -6,6 +6,54 @@ trait TaxonomyTrait { + /** + * Get related vocabulary + */ + + public function getTaxonomiesAttribute() + { + $relatedGrouped = $this->related()->get()->groupBy('vocabulary_id'); + + $groupedTaxonomy = []; + + foreach ($relatedGrouped as $key => $relateds) { + // var_dump($taxonomy); + $vocabulary = Vocabulary::find($key); + + $groupedTaxonomy[$key]['name'] = $vocabulary->name; + $groupedTaxonomy[$key]['id'] = $vocabulary->id; + + $groupedTaxonomy[$key]['terms'] = []; + + foreach ($relateds as $related) { + + $term = $related->term; + + $termData = [ + 'id' => $term->id, + 'name' => $term->name, + 'description' => $term->description, + 'root_term_id' => false, + 'root_term_name' => false, + 'root_term_description' => false + ]; + + $root = $term->root(); + + if( $root->id != $term->id ) + { + $termData['root_term_id'] = $root->id; + $termData['root_term_name'] = $root->name; + $termData['root_term_description'] = $root->description; + } + + array_push( $groupedTaxonomy[$key]['terms'], $termData ); + } + + } + + return $this->attributes['taxonomies'] = $groupedTaxonomy; + } /** * Return collection of tags related to the tagged model * From 88a80a660a71a4fa114f8e1d8b648114757dbdbb Mon Sep 17 00:00:00 2001 From: Todiadiyatmo Date: Thu, 7 Apr 2016 11:09:24 +0700 Subject: [PATCH 17/24] _ --- src/TaxonomyTrait.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/TaxonomyTrait.php b/src/TaxonomyTrait.php index 6e38a4d..da70902 100644 --- a/src/TaxonomyTrait.php +++ b/src/TaxonomyTrait.php @@ -17,13 +17,16 @@ public function getTaxonomiesAttribute() $groupedTaxonomy = []; foreach ($relatedGrouped as $key => $relateds) { - // var_dump($taxonomy); + $vocabulary = Vocabulary::find($key); - $groupedTaxonomy[$key]['name'] = $vocabulary->name; - $groupedTaxonomy[$key]['id'] = $vocabulary->id; + $slug = str_slug($vocabulary->name,'_'); + + $groupedTaxonomy[$slug] = []; + // $groupedTaxonomy[$key]['name'] = $vocabulary->name; + // $groupedTaxonomy[$key]['id'] = $vocabulary->id; - $groupedTaxonomy[$key]['terms'] = []; + // $groupedTaxonomy[$key]['terms'] = []; foreach ($relateds as $related) { @@ -47,7 +50,7 @@ public function getTaxonomiesAttribute() $termData['root_term_description'] = $root->description; } - array_push( $groupedTaxonomy[$key]['terms'], $termData ); + array_push( $groupedTaxonomy[$slug], $termData ); } } From 2365b2e89919cb01aef8bd565a067195b8879a56 Mon Sep 17 00:00:00 2001 From: Todi Adiyatmo Date: Mon, 11 Apr 2016 11:30:49 +0700 Subject: [PATCH 18/24] update whereHasTerm --- src/TaxonomyTrait.php | 134 +++++++++++++++++++++++------------------- 1 file changed, 74 insertions(+), 60 deletions(-) diff --git a/src/TaxonomyTrait.php b/src/TaxonomyTrait.php index da70902..ceba18d 100644 --- a/src/TaxonomyTrait.php +++ b/src/TaxonomyTrait.php @@ -12,50 +12,50 @@ trait TaxonomyTrait { public function getTaxonomiesAttribute() { - $relatedGrouped = $this->related()->get()->groupBy('vocabulary_id'); + $relatedGrouped = $this->related()->get()->groupBy('vocabulary_id'); - $groupedTaxonomy = []; + $groupedTaxonomy = []; - foreach ($relatedGrouped as $key => $relateds) { + foreach ($relatedGrouped as $key => $relateds) { - $vocabulary = Vocabulary::find($key); + $vocabulary = Vocabulary::find($key); - $slug = str_slug($vocabulary->name,'_'); + $slug = str_slug($vocabulary->name,'_'); - $groupedTaxonomy[$slug] = []; + $groupedTaxonomy[$slug] = []; // $groupedTaxonomy[$key]['name'] = $vocabulary->name; // $groupedTaxonomy[$key]['id'] = $vocabulary->id; // $groupedTaxonomy[$key]['terms'] = []; - foreach ($relateds as $related) { + foreach ($relateds as $related) { - $term = $related->term; + $term = $related->term; - $termData = [ - 'id' => $term->id, - 'name' => $term->name, - 'description' => $term->description, - 'root_term_id' => false, - 'root_term_name' => false, - 'root_term_description' => false - ]; + $termData = [ + 'id' => $term->id, + 'name' => $term->name, + 'description' => $term->description, + 'root_term_id' => false, + 'root_term_name' => false, + 'root_term_description' => false + ]; - $root = $term->root(); + $root = $term->root(); - if( $root->id != $term->id ) - { - $termData['root_term_id'] = $root->id; - $termData['root_term_name'] = $root->name; - $termData['root_term_description'] = $root->description; - } - - array_push( $groupedTaxonomy[$slug], $termData ); + if( $root->id != $term->id ) + { + $termData['root_term_id'] = $root->id; + $termData['root_term_name'] = $root->name; + $termData['root_term_description'] = $root->description; } + array_push( $groupedTaxonomy[$slug], $termData ); } - return $this->attributes['taxonomies'] = $groupedTaxonomy; + } + + return $this->attributes['taxonomies'] = $groupedTaxonomy; } /** * Return collection of tags related to the tagged model @@ -109,7 +109,7 @@ public function setTerm($term, $description="") { * The TermRelation object */ public function addTerm($term, $description="") { - + $term = ($term instanceof Term) ? $term : Term::findOrFail($term); if(!$term) @@ -119,9 +119,9 @@ public function addTerm($term, $description="") { return $this->updateTerm($term,$description); $term_relation = [ - 'term_id' => $term->id, - 'vocabulary_id' => $term->vocabulary_id, - 'description' => $description, + 'term_id' => $term->id, + 'vocabulary_id' => $term->vocabulary_id, + 'description' => $description, ]; return $this->related()->save(new TermRelation($term_relation)); @@ -248,22 +248,28 @@ public function removeTerms($vocabulary=false) { * * @return void */ - public function scopeWhereHasTerm($query, $term_id) { - return $query->whereHas('related', function($q) use($term_id) { - - if( method_exists($term_id,'toArray') ) - $term_id = $term_id->toArray(); - - if (is_array($term_id)) { - - - - $q->whereIn('term_id', $term_id); - } - else { - $q->where('term_id', '=', $term_id); - } - }); + public function scopeWhereHasTerm($query, $term_id) + { + if( is_int($term_id) ) + { + return $query->whereHas('related', function($q) use($term_id) + { + $q->where('term_id', '=', $term_id ); + }); + } + + if( method_exists($term_id,'toArray') ) + $term_id = $term_id->toArray(); + + foreach ($term_id as $single_term_id) + { + $query = $query->whereHas('related', function($q) use($single_term_id) + { + $q->where('term_id', '=', $single_term_id ); + }); + } + + return $query; } /** @@ -274,19 +280,27 @@ public function scopeWhereHasTerm($query, $term_id) { * * @return void */ - public function scopeWhereHasVocabulary($query, $vocabulary_id) { - return $query->whereHas('related', function($q) use($vocabulary_id) { - - if( method_exists($vocabulary_id,'toArray') ) - $vocabulary_id = $vocabulary_id->toArray(); - - if (is_array($vocabulary_id)) { - - $q->whereIn('vocabulary_id', $vocabulary_id); - } - else { - $q->where('vocabulary_id', '=', $vocabulary_id); - } - }); + public function scopeWhereHasVocabulary($query, $vocabulary_id) + { + if( is_int($vocabulary_id) ) + { + return $query->whereHas('related', function($q) use($vocabulary_id) + { + $q->where('vocabulary_id', '=', $vocabulary_id ); + }); + } + + if( method_exists($vocabulary_id,'toArray') ) + $vocabulary_id = $vocabulary_id->toArray(); + + foreach ($vocabulary_id as $single_vocabulary_id) + { + $query = $query->whereHas('related', function($q) use($single_vocabulary_id) + { + $q->where('vocabulary_id', '=', $single_vocabulary_id ); + }); + } + + return $query; } } From 700e870e9267e1f9e9c1be06eb8b9a28f3c0203c Mon Sep 17 00:00:00 2001 From: Todi Adiyatmo Date: Mon, 11 Apr 2016 14:51:46 +0700 Subject: [PATCH 19/24] fix is array --- src/TaxonomyTrait.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/TaxonomyTrait.php b/src/TaxonomyTrait.php index ceba18d..c9f992f 100644 --- a/src/TaxonomyTrait.php +++ b/src/TaxonomyTrait.php @@ -250,7 +250,9 @@ public function removeTerms($vocabulary=false) { */ public function scopeWhereHasTerm($query, $term_id) { - if( is_int($term_id) ) + + + if( !is_array($term_id) ) { return $query->whereHas('related', function($q) use($term_id) { @@ -282,7 +284,7 @@ public function scopeWhereHasTerm($query, $term_id) */ public function scopeWhereHasVocabulary($query, $vocabulary_id) { - if( is_int($vocabulary_id) ) + if( !is_array($vocabulary_id) ) { return $query->whereHas('related', function($q) use($vocabulary_id) { From 671171ec9b15896c9d3f92f462c5abc52ba93fa8 Mon Sep 17 00:00:00 2001 From: Todi Adiyatmo Date: Fri, 22 Apr 2016 16:57:10 +0700 Subject: [PATCH 20/24] fix --- src/Taxonomy.php | 46 ++++++++++++++++++---------------- src/TaxonomyTrait.php | 58 +++++++++++++++++++++++++++++++++++-------- 2 files changed, 71 insertions(+), 33 deletions(-) diff --git a/src/Taxonomy.php b/src/Taxonomy.php index 02f547d..08cf02e 100644 --- a/src/Taxonomy.php +++ b/src/Taxonomy.php @@ -59,7 +59,7 @@ public function getVocabulary($vocabulary) } /** - * Get a Vocabulary by name + * Get single term by name * * @param string $name * The name of the Vocabulary to fetch @@ -76,27 +76,7 @@ public function getTerm( $vocabulary, $name ) } /** - * Get a Vocabulary by name - * - * @param string $name - * The name of the Vocabulary to fetch - * - * @return - * The Vocabulary Model object, otherwise NULL - */ - public function getTermsByNameAsArray( $vocabulary , $field='name' ) - { - $vocabulary = $this->getVocabulary($vocabulary); - - if (!is_null($vocabulary)) { - return $vocabulary->terms->lists('name', 'id')->toArray(); - } - - return []; - } - - /** - * Get a Vocabulary by name + * Get a terms * * @param string $name * The name of the Vocabulary to fetch @@ -130,6 +110,28 @@ public function getTerms($vocabulary, $parentName = true, $withParent = false) return collect([]); } + /** + * Get a Vocabulary by name + * + * @param string $name + * The name of the Vocabulary to fetch + * + * @return + * The Vocabulary Model object, otherwise NULL + */ + public function getTermsByNameAsArray( $vocabulary , $field='name' ) + { + $vocabulary = $this->getVocabulary($vocabulary); + + if (!is_null($vocabulary)) { + return $vocabulary->terms->lists('name', 'id')->toArray(); + } + + return []; + } + + + /** * Get a Vocabulary by name as an options array for dropdowns * diff --git a/src/TaxonomyTrait.php b/src/TaxonomyTrait.php index c9f992f..4b31c10 100644 --- a/src/TaxonomyTrait.php +++ b/src/TaxonomyTrait.php @@ -151,6 +151,7 @@ public function updateTerm($term,$description="") { } + /** * Check if the Model instance has the passed term as an existing relation * @@ -161,17 +162,41 @@ public function updateTerm($term,$description="") { * The TermRelation object */ public function hasTerm($term) { - $term = ($term instanceof Term) ? $term : Term::findOrFail($term); - if(!$term) return false; - return ($this->related()->where('term_id', $term->id )->count()) ? TRUE : FALSE; } /** - * Get all the terms for a given vocabulary that are linked to the current + * Check if the Model instance has the passed term as an existing relation + * + * @param mixed $term_id + * The ID of the term or an instance of the Term object + * + * @return object + * The TermRelation object + */ + public function hasVocabulary($vocabulary, $name=false) { + + $vocabulary = \Devfactory\Taxonomy\Facades\TaxonomyFacade::getVocabulary($vocabulary); + + if(!$vocabulary) + return false; + + if(!$name) + return $this->related()->where('term_relations.vocabulary_id', $vocabulary->id)->first(); + + return $this ->related() + ->where('term_relations.vocabulary_id', $vocabulary->id) + ->whereHas('term', function($q) use($name) { + $q->where('term.name', '=', $name ); + }) + ->first(); + } + + /** + * Get all the term relations for a given vocabulary that are linked to the current * Model. * * @param $name string @@ -180,16 +205,23 @@ public function hasTerm($term) { * @return object * A collection of TermRelation objects */ - public function getTerms($vocabulary) { + public function getTerm($vocabulary,$name=false) { $vocabulary = \Devfactory\Taxonomy\Facades\TaxonomyFacade::getVocabulary($vocabulary); if(!$vocabulary) - return; + return false; - return $this->related()->where('term_relations.vocabulary_id', $vocabulary->id)->get(); - } + if(!$name) + return $this->related()->where('term_relations.vocabulary_id', $vocabulary->id)->first(); + return $this ->related() + ->where('term_relations.vocabulary_id', $vocabulary->id) + ->whereHas('term', function($q) use($name) { + $q->where('term.name', '=', $name ); + }) + ->first(); + } /** * Get all the terms for a given vocabulary that are linked to the current * Model. @@ -200,12 +232,16 @@ public function getTerms($vocabulary) { * @return object * A collection of TermRelation objects */ - public function getTerm($vocabulary) { + public function getTerms($vocabulary) { $vocabulary = \Devfactory\Taxonomy\Facades\TaxonomyFacade::getVocabulary($vocabulary); - return $this->related()->where('term_relations.vocabulary_id', $vocabulary->id)->first(); - } + if(!$vocabulary) + return; + + return $this->related()->where('term_relations.vocabulary_id', $vocabulary->id)->get(); + } + /** From 4889e744d0820847145512d25a5a9aa5b8c96f69 Mon Sep 17 00:00:00 2001 From: Todi Adiyatmo Date: Mon, 25 Apr 2016 00:44:56 +0700 Subject: [PATCH 21/24] add related function --- src/TaxonomyTrait.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/TaxonomyTrait.php b/src/TaxonomyTrait.php index 4b31c10..313cc2d 100644 --- a/src/TaxonomyTrait.php +++ b/src/TaxonomyTrait.php @@ -195,6 +195,25 @@ public function hasVocabulary($vocabulary, $name=false) { ->first(); } + /** + * Get all the term relations for a given vocabulary that are linked to the current + * Model. + * + * @param $name string + * The name of the vocabulary + * + * @return object + * A collection of TermRelation objects + */ + public function getRelated($term,$name=false) { + + $term = ($term instanceof Term) ? $term : Term::findOrFail($term); + if(!$term) + return false; + + return $this->related()->where('term_id', $term->id )->first(); + } + /** * Get all the term relations for a given vocabulary that are linked to the current * Model. From f962753c7dbb88e9cfd0713d3f7e9a27836e95d9 Mon Sep 17 00:00:00 2001 From: Todiadiyatmo Date: Thu, 9 Jun 2016 10:23:07 +0700 Subject: [PATCH 22/24] change owner and package --- README.md | 5 +- composer.json | 11 ++-- sample.php | 178 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 184 insertions(+), 10 deletions(-) create mode 100644 sample.php diff --git a/README.md b/README.md index 938ec41..c0699eb 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,8 @@ -r[![Build Status](https://travis-ci.org/DevFactoryCH/taxonomy.svg?branch=master)](https://travis-ci.org/DevFactoryCH/taxonomy) -[![Latest Stable Version](https://poser.pugx.org/devfactory/taxonomy/v/stable.svg)](https://packagist.org/packages/devfactory/taxonomy) -[![Total Downloads](https://poser.pugx.org/devfactory/taxonomy/downloads.svg)](https://packagist.org/packages/devfactory/taxonomy) [![License](https://poser.pugx.org/devfactory/taxonomy/license.svg)](https://packagist.org/packages/devfactory/taxonomy) #Taxonomy -This package allows you to create vocabularies with terms in Laravel 4 and 5 +This package allows you to create vocabularies with terms in Laravel 5 ## Installation diff --git a/composer.json b/composer.json index c97103f..201cfa9 100644 --- a/composer.json +++ b/composer.json @@ -1,9 +1,13 @@ { - "name": "devfactory/taxonomy", + "name": "tonjoo/taxonomy", "description": "Create and manage a heirarchical taxonomy of terms within different vocabularies", "license": "MIT", "keywords": ["taxonomy"], "authors": [ + { + "name": "Todi Adiyatmo Wijoyo", + "email": "t@tonjoo.com" + }, { "name": "Mark Cameron", "email": "mark.cameron@devfactory.ch" @@ -21,10 +25,5 @@ "psr-4": { "Devfactory\\Taxonomy\\": "src/" } - }, - "extra": { - "branch-alias": { - "dev-master": "2.0.x-dev" - } } } diff --git a/sample.php b/sample.php new file mode 100644 index 0000000..c5cf9e1 --- /dev/null +++ b/sample.php @@ -0,0 +1,178 @@ +truncate(); + DB::table('terms')->truncate(); + DB::table('vocabularies')->truncate(); + DB::table('term_relations')->truncate(); + DB::statement("SET foreign_key_checks=1"); + + Taxonomy::createVocabulary('Region'); + + $vocabulary = Taxonomy::getVocabulary(1); + + if( $vocabulary->name === "Region") + echo "true 1
"; + + // Using ID + $vocabulary2 = Taxonomy::getVocabulary('Region'); // Using Name + + if( $vocabulary2 == $vocabulary ) + echo "true 2
"; + + $termAsia = Taxonomy::createTerm('Region', [ + 'name' => 'Asia', + 'description'=>'description', + ]); + + $termEurope = Taxonomy::createTerm('Region', [ + 'name' => 'Europe', + 'description'=>'description', + ]); + + $termIndonesia = Taxonomy::createTerm('Region', [ + 'name' => 'Indonesia', + 'description'=>'description', + 'parent_id'=>$termAsia->id + ]); + + $termSingapore = Taxonomy::createTerm('Region', [ + 'name' => 'Singapore', + 'description'=>'description', + 'parent_id'=>$termAsia->id + ]); + + + $termFrance = Taxonomy::createTerm('Region', [ + 'name' => 'France', + 'description'=>'description', + 'parent_id'=>$termEurope->id + ]); + + $termGermany = Taxonomy::createTerm('Region', [ + 'name' => 'Germany', + 'description'=>'description', + 'parent_id'=>$termEurope->id + ]); + + if( $termAsia->name === 'Asia' && $termAsia->vocabulary_id === $vocabulary->id ) + echo "true 3
"; + + if( $termEurope->name === 'Europe' && $termEurope->vocabulary_id === $vocabulary->id ) + echo "true 4
"; + + if( $termIndonesia->name === 'Indonesia' && $termIndonesia->vocabulary_id === $vocabulary->id ) + echo "true 5
"; + + if( $termSingapore->name === 'Singapore' && $termSingapore->vocabulary_id === $vocabulary->id ) + echo "true 6
"; + + if( $termFrance->name === 'France' && $termFrance->vocabulary_id === $vocabulary->id ) + echo "true 7
"; + + if( $termGermany->name === 'Germany' && $termGermany->vocabulary_id === $vocabulary->id ) + echo "true 8
"; + + $terms = Vocabulary::where('name','Region')->first()->terms; // Using Name + + if( count($terms) === 6 ) + echo "true 9
"; + + $termGetAsia = Taxonomy::getTerm('Region', 'Asia'); + + if( $termGetAsia->name == $termAsia->name ) + echo "true 10
"; + + $termsGet = Taxonomy::getTerms('Region'); + + if( count($termsGet) == count($terms) ) + echo "true 11
"; + + $listTermId = Taxonomy::getTerms('Region')->pluck('id')->toArray(); + + if( in_array($termAsia->id, $listTermId) && in_array($termEurope->id, $listTermId) && in_array($termIndonesia->id, $listTermId) ) + echo "true 12
"; + + $listTermIdAsia = Taxonomy::getTerms('Region','Asia')->pluck('id')->toArray(); + + if( in_array($termIndonesia->id, $listTermIdAsia) && in_array($termSingapore->id, $listTermIdAsia) ) + echo "true 13
"; + + + $productOne = Product::create([ + 'name'=>'Lalala', + 'price'=>1 + ]); + + $productTwo = Product::create([ + 'name'=>'Tototo', + 'price'=>2 + ]); + + + $productThree = Product::create([ + 'name'=>'Rururu', + 'price'=>2 + ]); + + + $productOne->setTerm($termIndonesia); + + // reset term + $productOne->setTerm($termAsia); + + if( @$productOne->hasTerm($termAsia) == true ) + echo "true 14
"; + + if( @$productOne->hasTerm($termIndonesia) == false ) + echo "true 15
"; + + if( @$productOne->getTerm('Region')->term->id == $termAsia->id ) + echo "true 16
"; + + // reset term + $productOne->removeTerm($termAsia); + + if( @$productOne->hasTerm($termAsia) == false ) + echo "true 17
"; + + $productOne->addTerm($termAsia); + $productOne->addTerm($termIndonesia); + $productOne->addTerm($termSingapore); + + $productTwo->addTerm($termAsia); + $productTwo->addTerm($termIndonesia); + $productTwo->addTerm($termSingapore); + + $productThree->addTerm($termAsia); + $productThree->addTerm($termIndonesia); + $productThree->addTerm($termSingapore); + + if( count($productOne->getTerms('Region')) == 3 ) + echo "true 18
"; + + + $childOfAsia = Taxonomy::getTerms('Region','Asia')->pluck('id'); + + if( count($childOfAsia) == 2 ) + echo "true 19
"; + + $productsFromTerm = Product::whereHasTerm($childOfAsia)->get(); + + if( count($productsFromTerm) == 3 ) + echo "true 20
"; + + $vocabularyRegion = Taxonomy::getVocabulary('Region'); + + $productsFromVocabulary = Product::whereHasVocabulary($vocabularyRegion->id)->get(); + + if( count($productsFromVocabulary) == 3 ) + echo "true 21
"; + +}); \ No newline at end of file From 74d008537a3a540f6e6f87e61e19a68d524974d0 Mon Sep 17 00:00:00 2001 From: Todiadiyatmo Date: Thu, 9 Jun 2016 10:24:23 +0700 Subject: [PATCH 23/24] fix readme --- README.md | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c0699eb..487264b 100644 --- a/README.md +++ b/README.md @@ -9,11 +9,18 @@ This package allows you to create vocabularies with terms in Laravel 5 ### Laravel 5 In your `composer.json` add: -m + "require": { - "devfactory/taxonomy": "3.0.*" + "tonjoo/taxonomy": "master" } + "repositories": [ + { + "url": "https://github.com/todiadiyatmo/taxonomy.git", + "type": "git" + } + ] + From the terminal run composer update From 12eaa261f581fab417f4b17b62c9be14fb75c5b0 Mon Sep 17 00:00:00 2001 From: Todiadiyatmo Date: Thu, 9 Jun 2016 11:23:11 +0700 Subject: [PATCH 24/24] add default value --- src/Models/Term.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Models/Term.php b/src/Models/Term.php index 4a9972f..8718aba 100755 --- a/src/Models/Term.php +++ b/src/Models/Term.php @@ -4,6 +4,12 @@ class Term extends \Eloquent { protected $hidden = ['created_at','updated_at']; + + protected $attributes = [ + 'weight' => 0, + 'parent_id' => 0, + ]; + protected $fillable = [ 'name', 'vocabulary_id',