Skip to content

tmsllc/laravel-model-note

Repository files navigation

Add notes to Eloquent models

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 \TMSLLC\ModelNotes\Note

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

Contact Me

You can check all of my information by Checking my website.

Installation

You can install the package via composer:

composer require tmsllc/laravel-model-note

You must publish the migration with:

php artisan vendor:publish --provider="TMSLLC\ModelNote\ModelNoteServiceProvider" --tag="migrations"

Migrate the notes table:

php artisan migrate

Optionally you can publish the config-file with:

php artisan vendor:publish --provider="TMSLLC\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 `TMSLLC\ModelNote\Note`.
     */
    'note_model' => TMSLLC\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.

use TMSLLC\ModelNote\HasNotes;

class YourEloquentModel extends Model
{
    use HasNotes;
}

Add a new note

You can add a new note like this:

$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:

$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:

$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:

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

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

//or alternatively
$model->lastNote(); // returns the last instance of `TMSLLC\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");

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:

//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");

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:

$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.

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

You are welcome to contribute

Credits

This package was originally written by: DigitalCloud.

License

The MIT License (MIT). Please see License File for more information.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages