Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
MoamenEltouny committed Jul 10, 2022
1 parent 62362d3 commit 3f99c42
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/Models/Actions/Comment.php
@@ -0,0 +1,38 @@
<?php

namespace Pharaonic\Laravel\Users\Models\Actions;

use Illuminate\Database\Eloquent\Model;
use Pharaonic\Laravel\Users\Traits\Actions\Comment\isCommentable;

class Comment extends Model
{
use isCommentable;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'commentable_type', 'commentable_id',
'commenter_type', 'commenter_id',
'comment'
];

/**
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function commentable()
{
return $this->morphTo();
}

/**
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function commenter()
{
return $this->morphTo();
}
}
56 changes: 56 additions & 0 deletions src/Traits/Actions/Comment/isCommentable.php
@@ -0,0 +1,56 @@
<?php

namespace Pharaonic\Laravel\Users\Traits\Actions\Comment;

use Illuminate\Database\Eloquent\Model;
use Pharaonic\Laravel\Users\Models\Actions\Comment;

trait isCommentable
{
/**
* Comment with a Model
*
* @param Model $commenter
* @param string $comment
* @return boolean
*/
public function commentBy(Model $commenter, string $body)
{
return $this->comments()->make(['commentable' => $this, 'comment' => $body])->commenter()->associate($commenter)->save();
}

/**
* Uncomment with a Model
*
* @param Model $commenter
* @return boolean
*/
public function unCommentBy(Model $commenter)
{
if ($comment = $this->comments()->where(['commenter_id' => $commenter->getKey(), 'commenter_type' => get_class($commenter)])->first())
return $comment->delete();

return false;
}

/**
* Has Commented By Commenter
*
* @param Model $commenter
* @return boolean
*/
public function commentedBy(Model $commenter)
{
return $this->comments()->where(['commenter_id' => $commenter->getKey(), 'commenter_type' => get_class($commenter)])->exists();
}

/**
* Comments List
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}
}
56 changes: 56 additions & 0 deletions src/Traits/Actions/Comment/isCommenter.php
@@ -0,0 +1,56 @@
<?php

namespace Pharaonic\Laravel\Users\Traits\Actions\Comment;

use Illuminate\Database\Eloquent\Model;
use Pharaonic\Laravel\Users\Models\Actions\Comment;

trait isCommenter
{
/**
* Comment Model
*
* @param Model $commentable
* @param string $comment
* @return boolean
*/
public function comment(Model $commentable, string $body)
{
return $this->comments()->make(['commenter' => $this, 'comment' => $body])->commentable()->associate($commentable)->save();
}

/**
* Uncomment Model
*
* @param Model $commentable
* @return boolean
*/
public function uncomment(Model $commentable)
{
if ($comment = $this->comments()->where(['commentable_id' => $commentable->getKey(), 'commentable_type' => get_class($commentable)])->first())
return $comment->delete();

return false;
}

/**
* Has Commented By Commenter
*
* @param Model $commentable
* @return boolean
*/
public function commented(Model $commentable)
{
return $this->comments()->where(['commentable_id' => $commentable->getKey(), 'commentable_type' => get_class($commentable)])->exists();
}

/**
* Comments List
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function comments()
{
return $this->morphMany(Comment::class, 'commenter');
}
}
1 change: 1 addition & 0 deletions src/UsersServiceProvider.php
Expand Up @@ -53,6 +53,7 @@ public function boot()
__DIR__ . '/database/migrations/2021_02_01_000081_create_votes_table.php' => database_path('migrations/2021_02_01_000081_create_votes_table.php'),
__DIR__ . '/database/migrations/2021_02_01_000082_create_reviews_table.php' => database_path('migrations/2021_02_01_000082_create_reviews_table.php'),
__DIR__ . '/database/migrations/2021_02_01_000083_create_subscribes_table.php' => database_path('migrations/2021_02_01_000083_create_subscribes_table.php'),
__DIR__ . '/database/migrations/2021_02_01_000084_create_comments_table.php' => database_path('migrations/2021_02_01_000084_create_comments_table.php'),

], ['pharaonic', 'laravel-users']);

Expand Down
@@ -0,0 +1,40 @@
<?php

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

class CreateCommentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->bigIncrements('id');

$table->string('commentable_id');
$table->string('commentable_type');

$table->string('commenter_id');
$table->string('commenter_type');

$table->mediumText('comment');

$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('comments');
}
}

0 comments on commit 3f99c42

Please sign in to comment.