This package provides a HasNotes
trait that, once installed on a model, allows you to do things like this:
// 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
You can check all of my information by Checking my website.
You can install the package via composer:
composer require fahedaljghine/laravel-model-note
You must publish the migration with:
php artisan vendor:publish --provider="Fahedaljghine\ModelNote\ModelNoteServiceProvider" --tag="migrations"
Migrate the notes
table:
php artisan migrate
Optionally you can publish the config-file with:
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
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',
];
Add the HasNotes
trait to a model you like to use notes on.
use Fahedaljghine\ModelNote\HasNotes;
class YourEloquentModel extends Model
{
use HasNotes;
}
You can add a new note like this:
$model->addNote('whatever you like');
You can add a new private note which can be seen only be you like this:
$model->addNote('whatever you like' , true);
//or alternatively
$model->addPrivateNote('whatever you like');
Sometimes you will need to tag your note with some tag which can be done like this:
$model->addNote('whatever you like' , false , "tag1");
//or for the private note
$model->addPrivateNote('whatever you like' , "tag2");
You can get the last note of model:
$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:
$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:
//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:
//specific tag
$all_notes = $model->privateNotes("tag1");
//specific tags
$all_notes = $model->privateNotes("tag1" , "tag2");
You can delete any note that has been added on the model by id at any time by using the deleteNote
method:
//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:
//specific tag
$model->deleteNoteByTag("tag1");
//specific tags
$model->deleteNoteByTag("tag1", "tag2", "tag3");
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:
$model->deleteAllNotes();
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.
Please see CHANGELOG for more information what has changed recently.
You are welcome to contribute
The MIT License (MIT). Please see License File for more information.