Easily add notes to your Eloquent models.
You can install the package via composer:
composer require rumspeed/laravel-notesYou can publish and run the migrations with:
php artisan vendor:publish --tag="notes-migrations"
php artisan migrateYou can publish the config file with:
php artisan vendor:publish --tag="notes-config"This is the contents of the published config file:
return [
/* -----------------------------------------------------------------
| Models
| -----------------------------------------------------------------
*/
'authors' => [
'table' => 'users',
'model' => App\Models\User::class,
],
'notes' => [
'table' => 'notes',
'model' => Rumspeed\LaravelNotes\Models\Note::class,
],
];First things first, edit your eloquent model by using the Rumspeed\LaravelNotes\Traits\HasManyNotes trait.
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
use Rumspeed\LaravelNotes\Traits\HasManyNotes;
class Post extends Model {
use HasManyNotes;
// Other stuff ...
}You can also use the Rumspeed\LaravelNotes\Traits\HasOneNote trait if you want to manage one note for your model.
You can call the createNote() method on your Eloquent model like below:
$post = App\Post::first();
$note = $post->createNote('Hello world #1');$user = App\User::first();
$post = App\Post::first();
$note = $post->createNote('Hello world #1', $user);You can also specify how you want to add the author id by using the getCurrentAuthorId():
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
use Rumspeed\LaravelNotes\Traits\HasManyNotes;
class Post extends Model {
use HasManyNotes;
// Other stuff ...
/**
* Get the current author's id.
*
* @return int|null
*/
protected function getCurrentAuthorId()
{
return auth()->id();
}
}$post = App\Post::first();
$notes = $post->notes;NOTE :
$post->notesrelation property is only available in theHasManyNotestrait. If you're usingHasOneNotetrait, use$post->noteinstead.
You can also retrieve all the author's notes by using the Rumspeed\LaravelNotes\Traits\AuthoredNotes Trait in your User model (for example).
$user = App\User::first();
$post = App\Post::first();
$post->createNote('Hello world #1', $user);
$notes = $user->authoredNotes;$post = App\Post::first();
$note = $post->findNote(1);NOTE : The
findNote()method is only available in theHasManyNotestrait.
composer testPlease see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.