Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
62362d3
commit 3f99c42
Showing
5 changed files
with
191 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/database/migrations/2021_02_01_000084_create_comments_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
} | ||
} |