├── LICENSE.md ├── README.md ├── composer.json ├── config └── notes.php ├── database └── migrations │ └── 2016_10_00_000001_create_notes_table.php └── src ├── Database └── Migration.php ├── LaravelNotes.php ├── LaravelNotesServiceProvider.php ├── Models └── Note.php └── Traits ├── AuthoredNotes.php ├── HasManyNotes.php └── HasOneNote.php /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) ARCANEDEV - Laravel Notes 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Notes [![Packagist License][badge_license]](LICENSE.md) [![For Laravel][badge_laravel]][link-github-repo] 2 | 3 | [![Github Workflow Status][badge_build]][link-github-status] 4 | [![Coverage Status][badge_coverage]][link-scrutinizer] 5 | [![Scrutinizer Code Quality][badge_quality]][link-scrutinizer] 6 | [![SensioLabs Insight][badge_insight]][link-insight] 7 | [![Github Issues][badge_issues]][link-github-issues] 8 | 9 | [![Packagist][badge_package]][link-packagist] 10 | [![Packagist Release][badge_release]][link-packagist] 11 | [![Packagist Downloads][badge_downloads]][link-packagist] 12 | 13 | *By [ARCANEDEV©](http://www.arcanedev.net/)* 14 | 15 | This Laravel Notes will allow you to add a notes system into your Laravel project. 16 | 17 | ## Features 18 | 19 | * A very flexible notes system. 20 | * Easy setup & configuration. 21 | * Well documented & IDE Friendly. 22 | * Well tested with maximum code quality. 23 | * Laravel `5.1` to `9.x` are supported. 24 | * Made with :heart: & :coffee:. 25 | 26 | ## Table of contents 27 | 28 | 1. [Installation and Setup](_docs/1-Installation-and-Setup.md) 29 | 2. [Configuration](_docs/2-Configuration.md) 30 | 3. [Usage](_docs/3-Usage.md) 31 | 32 | ## Contribution 33 | 34 | Any ideas are welcome. Feel free to submit any issues or pull requests, please check the [contribution guidelines](CONTRIBUTING.md). 35 | 36 | ## Security 37 | 38 | If you discover any security related issues, please email arcanedev.maroc@gmail.com instead of using the issue tracker. 39 | 40 | ## Credits 41 | 42 | - [ARCANEDEV][link-author] 43 | - [All Contributors][link-contributors] 44 | 45 | [badge_laravel]: https://img.shields.io/badge/For%20Laravel-5.1%20to%209.x-orange.svg?style=flat-square 46 | [badge_license]: https://img.shields.io/packagist/l/arcanedev/laravel-notes.svg?style=flat-square 47 | [badge_build]: https://img.shields.io/github/workflow/status/ARCANEDEV/LaravelNotes/run-tests?style=flat-square 48 | [badge_coverage]: https://img.shields.io/scrutinizer/coverage/g/ARCANEDEV/LaravelNotes.svg?style=flat-square 49 | [badge_quality]: https://img.shields.io/scrutinizer/g/ARCANEDEV/LaravelNotes.svg?style=flat-square 50 | [badge_insight]: https://img.shields.io/sensiolabs/i/5ff01c70-3ad7-42b5-8c7f-6825b3887118.svg?style=flat-square 51 | [badge_issues]: https://img.shields.io/github/issues/ARCANEDEV/LaravelNotes.svg?style=flat-square 52 | [badge_package]: https://img.shields.io/badge/package-arcanedev/laravel--notes-blue.svg?style=flat-square 53 | [badge_release]: https://img.shields.io/packagist/v/arcanedev/laravel-notes.svg?style=flat-square 54 | [badge_downloads]: https://img.shields.io/packagist/dt/arcanedev/laravel-notes.svg?style=flat-square 55 | 56 | [link-author]: https://github.com/arcanedev-maroc 57 | [link-github-repo]: https://github.com/ARCANEDEV/LaravelNotes 58 | [link-github-status]: https://github.com/ARCANEDEV/LaravelNotes/actions 59 | [link-github-issues]: https://github.com/ARCANEDEV/LaravelNotes/issues 60 | [link-contributors]: https://github.com/ARCANEDEV/LaravelNotes/graphs/contributors 61 | [link-packagist]: https://packagist.org/packages/arcanedev/laravel-notes 62 | [link-scrutinizer]: https://scrutinizer-ci.com/g/ARCANEDEV/LaravelNotes/?branch=master 63 | [link-insight]: https://insight.sensiolabs.com/projects/5ff01c70-3ad7-42b5-8c7f-6825b3887118 64 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "arcanedev/laravel-notes", 3 | "description": "Provides the ability to add notes to your Eloquent models in Laravel.", 4 | "keywords": ["arcanedev", "laravel", "notes", "noteable"], 5 | "homepage": "https://github.com/ARCANEDEV/LaravelNotes", 6 | "authors": [ 7 | { 8 | "name": "ARCANEDEV", 9 | "email": "arcanedev.maroc@gmail.com", 10 | "homepage": "https://github.com/arcanedev-maroc", 11 | "role": "Developer" 12 | } 13 | ], 14 | "type": "library", 15 | "license": "MIT", 16 | "require": { 17 | "php": "^8.0", 18 | "arcanedev/support": "^9.0" 19 | }, 20 | "require-dev": { 21 | "fakerphp/faker": "^1.9.1", 22 | "mockery/mockery": "^1.4.4", 23 | "laravel/framework": "^9.0", 24 | "orchestra/testbench-core": "^7.0", 25 | "phpunit/phpunit": "^9.5.10" 26 | }, 27 | "autoload": { 28 | "psr-4": { 29 | "Arcanedev\\LaravelNotes\\": "src/" 30 | } 31 | }, 32 | "autoload-dev": { 33 | "psr-4": { 34 | "Arcanedev\\LaravelNotes\\Tests\\": "tests/" 35 | } 36 | }, 37 | "scripts": { 38 | "test": "phpunit", 39 | "test:dox": "phpunit --testdox" 40 | }, 41 | "extra": { 42 | "branch-alias": { 43 | "dev-develop": "9.x-dev" 44 | }, 45 | "laravel": { 46 | "providers": [ 47 | "Arcanedev\\LaravelNotes\\LaravelNotesServiceProvider" 48 | ] 49 | } 50 | }, 51 | "config": { 52 | "optimize-autoloader": true, 53 | "preferred-install": "dist", 54 | "sort-packages": true 55 | }, 56 | "minimum-stability": "dev", 57 | "prefer-stable": true 58 | } 59 | -------------------------------------------------------------------------------- /config/notes.php: -------------------------------------------------------------------------------- 1 | [ 11 | 'connection' => env('DB_CONNECTION', 'mysql'), 12 | 13 | 'prefix' => null, 14 | ], 15 | 16 | /* ----------------------------------------------------------------- 17 | | Models 18 | | ----------------------------------------------------------------- 19 | */ 20 | 21 | 'authors' => [ 22 | 'table' => 'users', 23 | 'model' => App\User::class, 24 | ], 25 | 26 | 'notes' => [ 27 | 'table' => 'notes', 28 | 'model' => Arcanedev\LaravelNotes\Models\Note::class 29 | ], 30 | 31 | ]; 32 | -------------------------------------------------------------------------------- /database/migrations/2016_10_00_000001_create_notes_table.php: -------------------------------------------------------------------------------- 1 | 12 | * 13 | * @see \Arcanedev\LaravelNotes\Models\Note 14 | */ 15 | return new class extends Migration 16 | { 17 | /* ----------------------------------------------------------------- 18 | | Constructor 19 | | ----------------------------------------------------------------- 20 | */ 21 | 22 | /** 23 | * CreateParticipantsTable constructor. 24 | */ 25 | public function __construct() 26 | { 27 | parent::__construct(); 28 | 29 | $this->setTable(config('notes.notes.table', 'notes')); 30 | } 31 | 32 | /* ----------------------------------------------------------------- 33 | | Main Methods 34 | | ----------------------------------------------------------------- 35 | */ 36 | 37 | /** 38 | * Run the migrations. 39 | */ 40 | public function up(): void 41 | { 42 | $this->createSchema(function (Blueprint $table) { 43 | $table->increments('id'); 44 | $table->text('content'); 45 | $table->morphs('noteable'); 46 | $table->unsignedBigInteger('author_id')->nullable(); 47 | $table->timestamps(); 48 | 49 | $table->foreign('author_id') 50 | ->references('id') 51 | ->on((string) config('notes.authors.table', 'users')) 52 | ->onDelete('cascade'); 53 | }); 54 | } 55 | }; 56 | -------------------------------------------------------------------------------- /src/Database/Migration.php: -------------------------------------------------------------------------------- 1 | 13 | */ 14 | abstract class Migration extends BaseMigration 15 | { 16 | /* ----------------------------------------------------------------- 17 | | Constructor 18 | | ----------------------------------------------------------------- 19 | */ 20 | 21 | /** 22 | * Migration constructor. 23 | */ 24 | public function __construct() 25 | { 26 | $this->setConnection(config('notes.database.connection')); 27 | $this->setPrefix(config('notes.database.prefix')); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/LaravelNotes.php: -------------------------------------------------------------------------------- 1 | 11 | */ 12 | class LaravelNotes 13 | { 14 | /* ----------------------------------------------------------------- 15 | | Properties 16 | | ----------------------------------------------------------------- 17 | */ 18 | 19 | /** 20 | * Publish the migrations. 21 | * 22 | * @var bool 23 | */ 24 | public static $publishMigrations = false; 25 | } 26 | -------------------------------------------------------------------------------- /src/LaravelNotesServiceProvider.php: -------------------------------------------------------------------------------- 1 | 13 | */ 14 | class LaravelNotesServiceProvider extends PackageServiceProvider 15 | { 16 | /* ----------------------------------------------------------------- 17 | | Properties 18 | | ----------------------------------------------------------------- 19 | */ 20 | 21 | /** 22 | * Package name. 23 | * 24 | * @var string 25 | */ 26 | protected $package = 'notes'; 27 | 28 | /* ----------------------------------------------------------------- 29 | | Main Methods 30 | | ----------------------------------------------------------------- 31 | */ 32 | 33 | /** 34 | * Register the service provider. 35 | */ 36 | public function register(): void 37 | { 38 | parent::register(); 39 | 40 | $this->registerConfig(); 41 | } 42 | 43 | /** 44 | * Boot the service provider. 45 | */ 46 | public function boot(): void 47 | { 48 | if ($this->app->runningInConsole()) { 49 | $this->publishConfig(); 50 | 51 | LaravelNotes::$publishMigrations ? $this->publishMigrations() : $this->loadMigrations(); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/Models/Note.php: -------------------------------------------------------------------------------- 1 | 14 | * 15 | * @property int $id 16 | * @property string $content 17 | * @property int $noteable_id 18 | * @property string $noteable_type 19 | * @property int $author_id 20 | * @property \Carbon\Carbon $created_at 21 | * @property \Carbon\Carbon $updated_at 22 | * 23 | * @property \Illuminate\Database\Eloquent\Model $author 24 | * @property \Illuminate\Database\Eloquent\Model $noteable 25 | */ 26 | class Note extends PrefixedModel 27 | { 28 | /* ----------------------------------------------------------------- 29 | | Properties 30 | | ----------------------------------------------------------------- 31 | */ 32 | 33 | /** 34 | * The attributes that are mass assignable. 35 | * 36 | * @var array 37 | */ 38 | protected $fillable = [ 39 | 'content', 40 | 'author_id', 41 | ]; 42 | 43 | /** 44 | * The attributes excluded from the model's JSON form. 45 | * 46 | * @var array 47 | */ 48 | protected $hidden = [ 49 | 'noteable_id', 50 | 'noteable_type', 51 | ]; 52 | 53 | /** 54 | * The attributes that should be cast to native types. 55 | * 56 | * @var array 57 | */ 58 | protected $casts = [ 59 | 'id' => 'integer', 60 | 'noteable_id' => 'integer', 61 | 'author_id' => 'integer', 62 | ]; 63 | 64 | /* ----------------------------------------------------------------- 65 | | Constructor 66 | | ----------------------------------------------------------------- 67 | */ 68 | 69 | /** 70 | * Note constructor. 71 | * 72 | * @param array $attributes 73 | */ 74 | public function __construct(array $attributes = []) 75 | { 76 | parent::__construct($attributes); 77 | 78 | $config = config('notes.database', []); 79 | 80 | $this->setConnection(Arr::get($config, 'connection')); 81 | $this->setPrefix(Arr::get($config, 'prefix')); 82 | $this->setTable(Arr::get($config, 'table', 'notes')); 83 | } 84 | 85 | /* ----------------------------------------------------------------- 86 | | Relationship 87 | | ----------------------------------------------------------------- 88 | */ 89 | 90 | /** 91 | * The noteable relationship. 92 | * 93 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo 94 | */ 95 | public function noteable() 96 | { 97 | return $this->morphTo(); 98 | } 99 | 100 | /** 101 | * The author relationship. 102 | * 103 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo 104 | */ 105 | public function author() 106 | { 107 | return $this->belongsTo(config('notes.authors.model')); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /src/Traits/AuthoredNotes.php: -------------------------------------------------------------------------------- 1 | 14 | * 15 | * @property \Illuminate\Database\Eloquent\Collection authoredNotes 16 | */ 17 | trait AuthoredNotes 18 | { 19 | /* ----------------------------------------------------------------- 20 | | Relationships 21 | | ----------------------------------------------------------------- 22 | */ 23 | 24 | /** 25 | * Relation to Many notes. 26 | * 27 | * @return \Illuminate\Database\Eloquent\Relations\HasMany 28 | */ 29 | public function authoredNotes(): HasMany 30 | { 31 | return $this->hasMany(config('notes.notes.model', Note::class), 'author_id'); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Traits/HasManyNotes.php: -------------------------------------------------------------------------------- 1 | 14 | * 15 | * @property \Illuminate\Database\Eloquent\Collection notes 16 | */ 17 | trait HasManyNotes 18 | { 19 | /* ----------------------------------------------------------------- 20 | | Relationships 21 | | ----------------------------------------------------------------- 22 | */ 23 | 24 | /** 25 | * The notes relationship. 26 | * 27 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 28 | */ 29 | public function notes(): MorphMany 30 | { 31 | return $this->morphMany((string) config('notes.notes.model'), 'noteable'); 32 | } 33 | 34 | /* ----------------------------------------------------------------- 35 | | Main Methods 36 | | ----------------------------------------------------------------- 37 | */ 38 | 39 | /** 40 | * Create a note. 41 | * 42 | * @param string $content 43 | * @param \Illuminate\Database\Eloquent\Model|null $author 44 | * @param bool $reload 45 | * 46 | * @return \Arcanedev\LaravelNotes\Models\Note 47 | */ 48 | public function createNote($content, $author = null, $reload = true) 49 | { 50 | /** @var \Arcanedev\LaravelNotes\Models\Note $note */ 51 | $note = $this->notes()->create( 52 | $this->prepareNoteAttributes($content, $author) 53 | ); 54 | 55 | if ($reload) { 56 | $relations = array_merge( 57 | ['notes'], 58 | method_exists($this, 'authoredNotes') ? ['authoredNotes'] : [] 59 | ); 60 | 61 | $this->load($relations); 62 | } 63 | 64 | return $note; 65 | } 66 | 67 | /** 68 | * Retrieve a note by its ID. 69 | * 70 | * @param int $id 71 | * 72 | * @return \Illuminate\Database\Eloquent\Model 73 | */ 74 | public function findNote($id) 75 | { 76 | return $this->notes()->find($id); 77 | } 78 | 79 | /* ----------------------------------------------------------------- 80 | | Other Methods 81 | | ----------------------------------------------------------------- 82 | */ 83 | 84 | /** 85 | * Prepare note attributes. 86 | * 87 | * @param string $content 88 | * @param \Illuminate\Database\Eloquent\Model|null $author 89 | * 90 | * @return array 91 | */ 92 | protected function prepareNoteAttributes($content, Model $author = null) 93 | { 94 | return [ 95 | 'author_id' => is_null($author) ? $this->getCurrentAuthorId() : $author->getKey(), 96 | 'content' => $content, 97 | ]; 98 | } 99 | 100 | /** 101 | * Get the current author's id. 102 | * 103 | * @return int|null 104 | */ 105 | protected function getCurrentAuthorId() 106 | { 107 | return null; 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /src/Traits/HasOneNote.php: -------------------------------------------------------------------------------- 1 | 13 | * 14 | * @property \Arcanedev\LaravelNotes\Models\Note note 15 | */ 16 | trait HasOneNote 17 | { 18 | /* ----------------------------------------------------------------- 19 | | Relationships 20 | | ----------------------------------------------------------------- 21 | */ 22 | 23 | /** 24 | * Relation to ONE note. 25 | * 26 | * @return \Illuminate\Database\Eloquent\Relations\MorphOne 27 | */ 28 | public function note() 29 | { 30 | return $this->morphOne(config('notes.notes.model'), 'noteable'); 31 | } 32 | 33 | /* ----------------------------------------------------------------- 34 | | Main Methods 35 | | ----------------------------------------------------------------- 36 | */ 37 | 38 | /** 39 | * Create a note. 40 | * 41 | * @param string $content 42 | * @param \Illuminate\Database\Eloquent\Model|null $author 43 | * @param bool $reload 44 | * 45 | * @return \Arcanedev\LaravelNotes\Models\Note 46 | */ 47 | public function createNote($content, $author = null, $reload = true) 48 | { 49 | if ($this->note) 50 | $this->note->delete(); 51 | 52 | /** @var \Arcanedev\LaravelNotes\Models\Note $note */ 53 | $note = $this->note()->create( 54 | $this->prepareNoteAttributes($content, $author) 55 | ); 56 | 57 | if ($reload) 58 | $this->load(['note']); 59 | 60 | return $note; 61 | } 62 | 63 | /** 64 | * Update a note. 65 | * 66 | * @param string $content 67 | * @param \Illuminate\Database\Eloquent\Model|null $author 68 | * @param bool $reload 69 | * 70 | * @return bool 71 | */ 72 | public function updateNote($content, Model $author = null, $reload = true) 73 | { 74 | $updated = $this->note->update( 75 | $this->prepareNoteAttributes($content, $author) 76 | ); 77 | 78 | if ($reload) $this->load(['note']); 79 | 80 | return $updated; 81 | } 82 | 83 | /* ----------------------------------------------------------------- 84 | | Other Methods 85 | | ----------------------------------------------------------------- 86 | */ 87 | 88 | /** 89 | * Prepare note attributes. 90 | * 91 | * @param string $content 92 | * @param \Illuminate\Database\Eloquent\Model|null $author 93 | * 94 | * @return array 95 | */ 96 | protected function prepareNoteAttributes($content, Model $author = null) 97 | { 98 | return [ 99 | 'author_id' => is_null($author) ? $this->getCurrentAuthorId() : $author->getKey(), 100 | 'content' => $content, 101 | ]; 102 | } 103 | 104 | /** 105 | * Get the current author's id. 106 | * 107 | * @return int|null 108 | */ 109 | protected function getCurrentAuthorId() 110 | { 111 | return null; 112 | } 113 | } 114 | --------------------------------------------------------------------------------