Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions .github/ISSUE_TEMPLATE/bug.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: Bug Report
description: Report an Issue or Bug with the Package
title: "[Bug]: "
labels: ["bug"]
body:
- type: markdown
attributes:
value: |
We're sorry to hear you have a problem. Can you help us solve it by providing the following details.
- type: textarea
id: what-happened
attributes:
label: What happened?
description: What did you expect to happen?
placeholder: I cannot currently do X thing because when I do, it breaks X thing.
validations:
required: true
- type: textarea
id: how-to-reproduce
attributes:
label: How to reproduce the bug
description: How did this occur, please add any config values used and provide a set of reliable steps if possible.
placeholder: When I do X I see Y.
validations:
required: true
- type: input
id: package-version
attributes:
label: Package Version
description: What version of our Package are you running? Please be as specific as possible
placeholder: 2.0.0
validations:
required: true
- type: input
id: php-version
attributes:
label: PHP Version
description: What version of PHP are you running? Please be as specific as possible
placeholder: 8.2.0
validations:
required: true
- type: input
id: laravel-version
attributes:
label: Laravel Version
description: What version of Laravel are you running? Please be as specific as possible
placeholder: 9.0.0
validations:
required: true
- type: dropdown
id: operating-systems
attributes:
label: Which operating systems does with happen with?
description: You may select more than one.
multiple: true
options:
- macOS
- Windows
- Linux
- type: textarea
id: notes
attributes:
label: Notes
description: Use this field to provide any other notes that you feel might be relevant to the issue.
validations:
required: false
9 changes: 3 additions & 6 deletions .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
blank_issues_enabled: false
contact_links:
- name: Ask a question
url: https://github.com/ryangjchandler/laravel-notes/discussions/new?category=q-a
url: https://github.com/alphaolomi/laravel-notes/discussions/new?category=q-a
about: Ask the community for help
- name: Request a feature
url: https://github.com/ryangjchandler/laravel-notes/discussions/new?category=ideas
url: https://github.com/alphaolomi/laravel-notes/discussions/new?category=ideas
about: Share ideas for new features
- name: Report a security issue
url: https://github.com/ryangjchandler/laravel-notes/security/policy
url: https://github.com/alphaolomi/laravel-notes/security/policy
about: Learn how to notify us for sensitive bugs
- name: Report a bug
url: https://github.com/ryangjchandler/laravel-notes/issues/new
about: Report a reproducable bug
16 changes: 10 additions & 6 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest]
php: [8.1]
laravel: [9.*]
php: [8.2, 8.1]
laravel: [10.*]
stability: [prefer-lowest, prefer-stable]
include:
- laravel: 9.*
testbench: 7.*
- laravel: 10.*
testbench: 8.*
carbon: ^2.63

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}

Expand All @@ -40,8 +41,11 @@ jobs:

- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" "nesbot/carbon:${{ matrix.carbon }}" --no-interaction --no-update
composer update --${{ matrix.stability }} --prefer-dist --no-interaction

- name: List Installed Dependencies
run: composer show -D

- name: Execute tests
run: vendor/bin/pest
run: vendor/bin/pest --ci
11 changes: 9 additions & 2 deletions config/notes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@

return [

/**
* The note model class name.
*/
'model' => \AlphaOlomi\Notes\Models\Note::class,

/** @phpstan-ignore-next-line */
'user' => \App\Models\User::class,
/**
* The user model class name. This is the model that will be used to
* represent the authors of the notes. By default this is the default
* user model that ships with Laravel.
*/
'user_model' => config('auth.providers.users.model')

];
64 changes: 63 additions & 1 deletion database/factories/NoteFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,74 @@

class NoteFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Note::class;

/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'content' => $this->faker->words(rand(3, 10), asText: true),
'parent_id' => null, // Set to null for a root note
'user_id' => config('notes.user_model')::factory(),
'notable_type' => null, // Replace with the desired polymorphic type
'notable_id' => null, // Replace with the desired polymorphic ID
'content' => $this->faker->paragraph(),
];
}

/**
* Indicate that the note has a parent.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
public function withParent()
{
return $this->state(function (array $attributes) {
return [
'parent_id' => Note::factory(),
];
});
}

/**
* Indicate the type and ID of the related polymorphic model.
*
* @param string $type
* @param int $id
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
public function withNotable($type, $id)
{
return $this->state(function (array $attributes) use ($type, $id) {
return [
'notable_type' => $type,
'notable_id' => $id,
];
});
}

/**
* Indicate the related polymorphic model.
*
* @param string $type
* @param int $id
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
public function withModelNotable($model)
{
return $this->state(function (array $attributes) use ($model) {
return [
'notable_type' => $model::class,
'notable_id' => $model::factory(),
];
});
}
}
10 changes: 7 additions & 3 deletions database/migrations/create_notes_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ public function up()
Schema::create('notes', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('parent_id')->nullable()->index();
$table->unsignedBigInteger('user_id')->index()->nullable();
$table->morphs('notable');
$table->longText('content');
$table->unsignedBigInteger('user_id')->nullable()->index();
$table->nullableMorphs('notable');
$table->string('title');
$table->longText('content')->nullable();
$table->timestamps();
$table->softDeletes();

// Add foreign key constraints
$table->foreign('parent_id')->references('id')->on('notes')->onDelete('set null');
});
}
};
70 changes: 66 additions & 4 deletions src/Concerns/HasNotes.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,84 @@
use Illuminate\Support\Facades\Auth;

/**
* Notes trait.
*
* @property-read MorphMany<IsNote> $notes
* @property-read MorphMany<IsNote> $allNotes
* @property-read MorphMany<IsNote> $rootNotes
* @property-read MorphMany<IsNote> $allRootNotes
* @property-read MorphMany<IsNote> $firstNote
*
* @mixin \Illuminate\Database\Eloquent\Model
*/
trait HasNotes
{
/** @return MorphMany<IsNote> */
/**
* Get all of the model's notes.
*
* @return MorphMany<IsNote>
*/
public function notes(): MorphMany
{
return $this->morphMany(config('notes.model'), 'notable');
}

public function addNote(string $content, Model $user = null, IsNote $parent = null): IsNote
/**
* Get all of the model's notes.
*
* @return MorphMany<IsNote>
*/
public function allNotes(): MorphMany
{
return $this->notes()->create([
return $this->notes()->with('children');
}

/**
* Get all of the model's notes.
*
* @return MorphMany<IsNote>
*/
public function rootNotes(): MorphMany
{
return $this->notes()->whereNull('parent_id');
}

/**
* Get all of the model's notes.
*
* @return MorphMany<IsNote>
*/
public function allRootNotes(): MorphMany
{
return $this->allNotes()->whereNull('parent_id');
}

/**
* Get first note of the model.
*
* @return MorphMany<IsNote>
*/
public function firstNote(): MorphMany
{
return $this->notes()->first();
}

/**
* Add a note to the model.
*
* @param string $content
* @param Model|null $user
* @param IsNote|null $parent
* @return IsNote
*/
public function addNote(string $content, Model $user = null, IsNote $parent = null)
{
$note = $this->notes()->create([
'content' => $content,
'user_id' => $user ? $user->getKey() : Auth::id(),
'parent_id' => $parent?->getKey(),
'parent_id' => $parent ? $parent?->getKey() : null,
]);

return $note;
}
}
7 changes: 7 additions & 0 deletions src/Contracts/IsNote.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
use Illuminate\Database\Eloquent\Relations\MorphTo;

/**
* Note contract.
*
* @property-read MorphTo $notes
* @property-read BelongsTo $parent
* @property-read HasMany $children
* @property-read BelongsTo $user
*
* @mixin \Illuminate\Database\Eloquent\Model
*/
interface IsNote
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Note.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function notes(): MorphTo

public function user(): BelongsTo
{
return $this->belongsTo(config('notes.user'), 'user_id');
return $this->belongsTo(config('notes.user_model'), 'user_id');
}

public function parent(): BelongsTo
Expand Down
Loading