-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e06e970
Showing
24 changed files
with
927 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
insert_final_newline = true | ||
indent_style = space | ||
indent_size = 4 | ||
trim_trailing_whitespace = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false | ||
|
||
[*.yml] | ||
indent_style = space | ||
indent_size = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
* text=auto | ||
|
||
/.github export-ignore | ||
/build export-ignore | ||
/tests export-ignore | ||
.editorconfig export-ignore | ||
.gitattributes export-ignore | ||
.gitignore export-ignore | ||
.travis.yml export-ignore | ||
CHANGELOG.md export-ignore | ||
CONTRIBUTING.md export-ignore | ||
README.md export-ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
composer.lock | ||
/build | ||
/vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
preset: laravel | ||
|
||
linting: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
language: php | ||
|
||
php: | ||
- 7.0 | ||
- 7.1 | ||
|
||
matrix: | ||
fast_finish: true | ||
|
||
sudo: false | ||
|
||
before_script: | ||
- travis_retry composer self-update | ||
- travis_retry composer install --prefer-source --no-interaction --dev | ||
|
||
script: | ||
- vendor/bin/phpunit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Release Notes | ||
|
||
All notable changes to `laravel-categorizable` will be documented in this file | ||
|
||
## 1.0.0 (2017-07-25) | ||
|
||
Initial release. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
CONTRIBUTING | ||
============ | ||
|
||
Contributions are welcome, and are accepted via pull requests. Please review these guidelines before submitting any pull requests. | ||
|
||
## Guidelines | ||
|
||
* Please follow the [PSR-2 Coding Style Guide](http://www.php-fig.org/psr/psr-2/), enforced by [StyleCI](https://styleci.io/). | ||
* Ensure that the current tests pass, and if you've added something new, add the tests where relevant. | ||
* Send a coherent commit history, making sure each individual commit in your pull request is meaningful. | ||
* You may need to [rebase](https://git-scm.com/book/en/v2/Git-Branching-Rebasing) to avoid merge conflicts. | ||
* If you are changing the behavior, or the public api, you may need to update the docs. | ||
* Please remember that we follow [SemVer](http://semver.org/). | ||
|
||
## Running Tests | ||
|
||
You will need an install of [Composer](https://getcomposer.org/) before continuing. | ||
|
||
First, install the dependencies: | ||
|
||
```bash | ||
$ composer install | ||
``` | ||
|
||
Then run PHPUnit: | ||
|
||
```bash | ||
$ vendor/bin/phpunit | ||
``` | ||
|
||
If the test suite passes on your local machine you should be good to go. | ||
|
||
When you make a pull request, the tests will automatically be run again by [Travis CI](https://travis-ci.org/). | ||
|
||
We also have [StyleCI](https://styleci.io/) setup to automatically fix any code style issues. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) Sercan Çakır | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
# Laravel Categorizable | ||
|
||
[](https://packagist.org/packages/mayoz/laravel-categorizable) | ||
[](LICENSE.md) | ||
[](https://travis-ci.org/mayoz/laravel-categorizable) | ||
[](https://styleci.io/repos/71335427) | ||
|
||
Easily add the ability to category your Eloquent models in Laravel 5. | ||
|
||
* [Installation](#installation) | ||
* [Configuration](#configuration) | ||
* [Usage](#usage) | ||
* [Extending](#extending) | ||
* [License](#license) | ||
|
||
## Installation | ||
|
||
You can install the package via composer: | ||
|
||
``` bash | ||
composer require mayoz/categorizable | ||
``` | ||
|
||
Register the service provider in your `config/app.php` configuration file: | ||
|
||
```php | ||
'providers' => [ | ||
... | ||
Mayoz\Categorizable\CategorizableServiceProvider::class, | ||
... | ||
]; | ||
``` | ||
|
||
You can publish the migration with: | ||
|
||
```bash | ||
php artisan vendor:publish --provider="Mayoz\Categorizable\CategorizableServiceProvider" --tag="migrations" | ||
``` | ||
|
||
The migration has been published you can create the `categories` and `categorizable` tables. You are feel free for added new fields that you need. After, run the migrations: | ||
|
||
```bash | ||
php artisan migrate | ||
``` | ||
|
||
## Usage | ||
|
||
Suppose, you have the `Post` model as follows: | ||
|
||
```php | ||
<?php | ||
|
||
namespace App; | ||
|
||
use Mayoz\Categorizable\Categorizable; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Post extends Model | ||
{ | ||
use Categorizable; | ||
} | ||
``` | ||
|
||
Associate new categories for the `Post` model: | ||
|
||
```php | ||
$post = Post::find(1); | ||
|
||
$post->categorize([1, 2, 3, 4, 5]); | ||
|
||
return $post; | ||
``` | ||
|
||
Now, the `post` model is associated with categories ids of `1`, `2`, `3`, `4` and `5`. | ||
|
||
Remove the existing category association for the `Post` model: | ||
|
||
```php | ||
$post = Post::find(1); | ||
|
||
$post->uncategorize([3, 5]); | ||
|
||
return $post; | ||
``` | ||
|
||
The `post` model is associated with categories ids of `1`, `2` and `4`. | ||
|
||
Rearrange the category relationships for the `Post` model: | ||
|
||
```php | ||
$post = Post::find(1); | ||
|
||
$post->recategorize([1, 5]); | ||
|
||
return $post; | ||
``` | ||
|
||
The `post` model is associated with categories ids of `1` and `5`. | ||
|
||
## Extending | ||
|
||
I suggest, you always extend the `Category` model to define your relationships directly. Create you own `Category` model: | ||
|
||
```php | ||
<?php | ||
|
||
namespace App; | ||
|
||
use Mayoz\Categorizable\Category as BaseCategory; | ||
|
||
class Category extends BaseCategory | ||
{ | ||
/** | ||
* Get all posts for the relation. | ||
* | ||
* @return \Illuminate\Database\Eloquent\Relations\MorphedByMany | ||
*/ | ||
public function posts() | ||
{ | ||
return $this->categorized(Post::class); | ||
} | ||
} | ||
``` | ||
|
||
You publish the package config: | ||
|
||
```bash | ||
php artisan vendor:publish --provider="Mayoz\Categorizable\CategorizableServiceProvider" --tag="config" | ||
``` | ||
|
||
This is the contents of the published config file: | ||
|
||
```php | ||
<?php | ||
|
||
return [ | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Model Namespace | ||
|-------------------------------------------------------------------------- | ||
| | ||
| Change these values when you need to extend the default category model | ||
| or if the user model needs to be served in a different namespace. | ||
| | ||
*/ | ||
|
||
'category' => App\Category::class, | ||
|
||
]; | ||
``` | ||
|
||
That is all. Now let's play for relationship query with the category. | ||
|
||
```php | ||
/** | ||
* Respond the post | ||
* | ||
* @param \App\Category $category | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function index(Category $category) | ||
{ | ||
return $category->posts()->paginate(10); | ||
} | ||
``` | ||
|
||
If we did not extend the `Category` model, as had to use; | ||
|
||
```php | ||
/** | ||
* Respond the post | ||
* | ||
* @param \App\Category $category | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function index(Category $category) | ||
{ | ||
return $category->categorize(Post::class)->paginate(10); | ||
} | ||
``` | ||
|
||
## License | ||
|
||
This package is licensed under [The MIT License (MIT)](LICENSE). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
{ | ||
"name": "mayoz/laravel-categorizable", | ||
"description": "Polymorphic categorizable for Laravel", | ||
"keywords": ["laravel", "polymorphic", "categorizable"], | ||
"license": "MIT", | ||
"support": { | ||
"issues": "https://github.com/mayoz/laravel-categorizable/issues", | ||
"source": "https://github.com/mayoz/laravel-categorizable" | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Sercan Çakır", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": "^7.0", | ||
"illuminate/database": "~5.4|~5.5", | ||
"illuminate/support": "~5.4|~5.5" | ||
}, | ||
"require-dev": { | ||
"orchestra/testbench": "^3.4", | ||
"phpunit/phpunit": "^6.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Mayoz\\Categorizable\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Mayoz\\Tests\\Categorizable\\": "tests" | ||
} | ||
}, | ||
"config": { | ||
"preferred-install": "dist", | ||
"sort-packages": true, | ||
"optimize-autoloader": true | ||
}, | ||
"extra": { | ||
"laravel": { | ||
"providers": [ | ||
"Mayoz\\Categorizable\\CategorizableServiceProvider" | ||
] | ||
} | ||
}, | ||
"minimum-stability": "dev", | ||
"prefer-stable": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
return [ | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Model Namespace | ||
|-------------------------------------------------------------------------- | ||
| | ||
| Change these values when you need to extend the default category model | ||
| or if the user model needs to be served in a different namespace. | ||
| | ||
*/ | ||
|
||
'category' => Mayoz\Categorizable\Category::class, | ||
|
||
]; |
Oops, something went wrong.