Skip to content

Commit

Permalink
initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
fahedtms committed May 8, 2022
1 parent ceda1ba commit 4ada291
Show file tree
Hide file tree
Showing 10 changed files with 589 additions and 2 deletions.
37 changes: 37 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

$finder = Symfony\Component\Finder\Finder::create()
->notPath('bootstrap/*')
->notPath('storage/*')
->notPath('resources/view/mail/*')
->in([
__DIR__ . '/src',
__DIR__ . '/tests',
])
->name('*.php')
->notName('*.blade.php')
->ignoreDotFiles(true)
->ignoreVCS(true);

return (new PhpCsFixer\Config)
->setRules([
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
'ordered_imports' => ['sort_algorithm' => 'alpha'],
'no_unused_imports' => true,
'not_operator_with_successor_space' => true,
'trailing_comma_in_multiline' => true,
'phpdoc_scalar' => true,
'unary_operator_spaces' => true,
'binary_operator_spaces' => true,
'blank_line_before_statement' => [
'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'],
],
'phpdoc_single_line_var_spacing' => true,
'phpdoc_var_without_name' => true,
'method_argument_space' => [
'on_multiline' => 'ensure_fully_multiline',
'keep_multiple_spaces_after_comma' => true,
]
])
->setFinder($finder);
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Changelog

All notable changes to `laravel-model-note` will be documented in this file

## 1.0.0 - 2022-05-08

- initial release
235 changes: 233 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,233 @@
# laravel-model-note
A package to enable assigning notes to Eloquent Models
# Add notes to Eloquent models

This package provides a `HasNotes` trait that, once installed on a model, allows you to do things like this:

```php
// add a note
$model->addNote('needs manager approve');

// add another note
$model->addNote('manager approved');

// get the current status
$model->notes(); // returns a collection of \Fahedaljghine\ModelNotes\Note

// get the last note
$lastNote = $model->lastNote(); // returns an instance of \Fahedaljghine\ModelNotes\Note

```

## Hire Me

You can hire me to help you get your business online, consulting, conflict solving, building up your development team
and whatever you need for a web solutions. You can check all of my information
by [Checking my website](https://fahedaljghine.com/).

## Installation

You can install the package via composer:

```bash
composer require fahedaljghine/laravel-model-note
```

You must publish the migration with:

```bash
php artisan vendor:publish --provider="Fahedaljghine\ModelNote\ModelNoteServiceProvider" --tag="migrations"
```

Migrate the `notes` table:

```bash
php artisan migrate
```

Optionally you can publish the config-file with:

```bash
php artisan vendor:publish --provider="Fahedaljghine\ModelNote\ModelNoteServiceProvider" --tag="config"
```

This is the contents of the file which will be published at `config/model-note.php`

```php
return [

/*
* The class name of the notes model that holds all notes.
*
* The model must be or extend `Fahedaljghine\ModelNote\Note`.
*/
'note_model' => Fahedaljghine\ModelNote\Note::class,

/*
* The name of the column which holds the ID of the model related to the notes.
*
* You can change this value if you have set a different name in the migration for the notes table.
*/
'model_primary_key_attribute' => 'model_id',

];
```

## Usage

Add the `HasNotes` trait to a model you like to use notes on.

```php
use Fahedaljghine\ModelNote\HasNotes;

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

### Add a new note

You can add a new note like this:

```php
$model->addNote('whatever you like');
```

### Add a private note

You can add a new private note which can be seen only be you like this:

```php
$model->addNote('whatever you like' , true);

//or alternatively
$model->addPrivateNote('whatever you like');

```

### Add a note with tag

Sometimes you will need to tag your note with some tag which can be done like this:

```php
$model->addNote('whatever you like' , false , "tag1");

//or for the private note
$model->addPrivateNote('whatever you like' , "tag2");

```

### Retrieving notes

You can get the last note of model:

```php
$model->note; // returns the text of the last note

$model->note(); // returns the last instance of `Fahedaljghine\ModelNote\Note`

//or alternatively
$model->lastNote(); // returns the last instance of `Fahedaljghine\ModelNote\Note`
```

All associated notes of a model can be retrieved like this:

```php
$all_notes = $model->notes;

//or alternatively
$all_notes = $model->notes();
```

All associated notes of a model with specific tag or tags can be retrieved like this:

```php

//last note of specific tag
$last_note = $model->lastNote("tag1");

//specific tag
$all_notes = $model->allNotes("tag1");

//specific tags
$all_notes = $model->allNotes("tag1" , "tag2");
```

All associated private notes of a model with specific tag or tags can be retrieved like this:

```php
//specific tag
$all_notes = $model->privateNotes("tag1");

//specific tags
$all_notes = $model->privateNotes("tag1" , "tag2");
```

### Delete a note from model

You can delete any note that has been added on the model by id at any time by using the `deleteNote` method:

```php
//specific id
$model->deleteNote(1);

//specific ides
$model->deleteNote(1, 2, 3);

```

You can delete any note that has been added on the model by tag at any time by using the `deleteNote` method:

```php
//specific tag
$model->deleteNoteByTag("tag1");

//specific tags
$model->deleteNoteByTag("tag1", "tag2", "tag3");

```

### Delete all notes from model

You can delete all notes that had been added on the model at any time by using the `deleteAllNotes` method:

Delete all notes from model:

```php
$model->deleteAllNotes();
```

### Custom model and migration

You can change the model used by specifying a class name in the `note_model` key of the `model-note` config file.

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

### Testing

This package contains integration tests that are powered
by [orchestral/testbench](https://github.com/orchestral/testbench).

You can run all tests with:

```bash
composer test
```

### Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

## Contributing

You are welcome to contribute

## Credits

- [Fahed Aljghine](https://github.com/fahedaljghine)
- [All Contributors](../../contributors)

## License

The MIT License (MIT). Please see [License File](LICENSE) for more information.
53 changes: 53 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"name": "fahedaljghine/laravel-model-note",
"description": "A package to enable assigning notes to Eloquent Models",
"keywords": [
"laravel",
"notes"
],
"homepage": "https://github.com/fahedaljghine/laravel-model-note",
"license": "MIT",
"authors": [
{
"name": "Fahed Aljghine",
"email": "[email protected]",
"homepage": "https://fahedaljghine.com",
"role": "Developer"
}
],
"require": {
"php": "^8.0",
"illuminate/support": "^8.71|^9.0"
},
"require-dev": {
"phpunit/phpunit": "^9.4",
"orchestra/testbench": "^6.23|^7.0"
},
"autoload": {
"psr-4": {
"Fahedaljghine\\ModelNote\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Fahedaljghine\\ModelNote\\Tests\\": "tests"
}
},
"scripts": {
"test": "vendor/bin/phpunit",
"test-coverage": "phpunit --coverage-html coverage"
},
"config": {
"sort-packages": true
},
"extra": {
"laravel": {
"providers": [
"Fahedaljghine\\ModelNote\\ModelNoteServiceProvider"
]
}
},
"minimum-stability": "dev",
"vendor/package": "1.0.0",
"prefer-stable": true
}
18 changes: 18 additions & 0 deletions config/model-note.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

return [

/*
* The class name of the status model that holds all statuses.
*
* The model must be or extend `Fahedaljghine\ModelNotes\Note`.
*/
'note_model' => Fahedaljghine\ModelNote\Note::class,

/*
* The name of the column which holds the ID of the model related to the statuses.
*
* You can change this value if you have set a different name in the migration for the statuses table.
*/
'model_primary_key_attribute' => 'model_id',
];
26 changes: 26 additions & 0 deletions database/migrations/create_notes_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateNotesTable extends Migration
{
public function up()
{
Schema::create('notes', function (Blueprint $table) {
$table->increments('id');
$table->text('text');
$table->string('tag')->nullable();
$table->boolean('is_private')->default(0);
$table->unsignedBigInteger('user_id');
$table->morphs('model');
$table->timestamps();
});
}

public function down()
{
Schema::dropIfExists('notes');
}
}
13 changes: 13 additions & 0 deletions src/Exceptions/InvalidNoteModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Fahedaljghine\ModelNote\Exceptions;

use Exception;

class InvalidNoteModel extends Exception
{
public static function create(string $model): self
{
return new self("The model `{$model}` is invalid. A valid model must extend the model \Fahedaljghine\ModelNote\Note.");
}
}
Loading

0 comments on commit 4ada291

Please sign in to comment.