-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate migrations & Implementing ability to like models (#8)
- Loading branch information
Showing
16 changed files
with
408 additions
and
19 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
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 | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class Blockers extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('blockers', function (Blueprint $table) { | ||
$table->increments('id'); | ||
|
||
$table->integer('blockable_id'); | ||
$table->string('blockable_type'); | ||
|
||
$table->integer('blocker_id')->nullable(); | ||
$table->string('blocker_type')->nullable(); | ||
|
||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('blockers'); | ||
} | ||
} |
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 | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class Likers extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('likers', function (Blueprint $table) { | ||
$table->increments('id'); | ||
|
||
$table->integer('likeable_id'); | ||
$table->string('likeable_type'); | ||
|
||
$table->integer('liker_id')->nullable(); | ||
$table->string('liker_type')->nullable(); | ||
|
||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('likers'); | ||
} | ||
} |
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
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
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,8 @@ | ||
<?php | ||
|
||
namespace Rennokki\Befriended\Contracts; | ||
|
||
interface Likeable | ||
{ | ||
public function likers($model = null); | ||
} |
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,14 @@ | ||
<?php | ||
|
||
namespace Rennokki\Befriended\Contracts; | ||
|
||
interface Liker | ||
{ | ||
public function liking($model = null); | ||
|
||
public function isLiking($model); | ||
|
||
public function like($model); | ||
|
||
public function unlike($model); | ||
} |
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,8 @@ | ||
<?php | ||
|
||
namespace Rennokki\Befriended\Contracts; | ||
|
||
interface Liking extends Likeable, Liker | ||
{ | ||
// | ||
} |
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,24 @@ | ||
<?php | ||
|
||
namespace Rennokki\Befriended\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class LikerModel extends Model | ||
{ | ||
protected $table = 'likers'; | ||
protected $fillable = [ | ||
'likeable_id', 'likeable_type', | ||
'liker_id', 'liker_type', | ||
]; | ||
|
||
public function likeable() | ||
{ | ||
return $this->morphTo(); | ||
} | ||
|
||
public function liker() | ||
{ | ||
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,14 @@ | ||
<?php | ||
|
||
namespace Rennokki\Befriended\Traits; | ||
|
||
trait CanBeLiked | ||
{ | ||
public function likers($model = null) | ||
{ | ||
return $this->morphToMany(($model) ?: $this->getMorphClass(), 'likeable', 'likers', 'likeable_id', 'liker_id') | ||
->withPivot('liker_type') | ||
->wherePivot('liker_type', ($model) ?: $this->getMorphClass()) | ||
->wherePivot('likeable_type', $this->getMorphClass()); | ||
} | ||
} |
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,58 @@ | ||
<?php | ||
|
||
namespace Rennokki\Befriended\Traits; | ||
|
||
use Rennokki\Befriended\Contracts\Liker; | ||
use Rennokki\Befriended\Contracts\Liking; | ||
|
||
trait CanLike | ||
{ | ||
public function liking($model = null) | ||
{ | ||
return $this->morphToMany(($model) ?: $this->getMorphClass(), 'liker', 'likers', 'liker_id', 'likeable_id') | ||
->withPivot('likeable_type') | ||
->wherePivot('likeable_type', ($model) ?: $this->getMorphClass()) | ||
->wherePivot('liker_type', $this->getMorphClass()); | ||
} | ||
|
||
public function isLiking($model) | ||
{ | ||
if (! $model instanceof Liker && ! $model instanceof Liking) { | ||
return false; | ||
} | ||
|
||
return (bool) ! is_null($this->liking($model->getMorphClass())->find($model->getKey())); | ||
} | ||
|
||
public function like($model) | ||
{ | ||
if (! $model instanceof Liker && ! $model instanceof Liking) { | ||
return false; | ||
} | ||
|
||
if ($this->isLiking($model)) { | ||
return false; | ||
} | ||
|
||
$this->liking()->attach($this->getKey(), [ | ||
'liker_id' => $this->getKey(), | ||
'likeable_type' => $model->getMorphClass(), | ||
'likeable_id' => $model->getKey(), | ||
]); | ||
|
||
return true; | ||
} | ||
|
||
public function unlike($model) | ||
{ | ||
if (! $model instanceof Liker && ! $model instanceof Liking) { | ||
return false; | ||
} | ||
|
||
if (! $this->isLiking($model)) { | ||
return false; | ||
} | ||
|
||
return (bool) $this->liking($model->getMorphClass())->detach($model->getKey()); | ||
} | ||
} |
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,8 @@ | ||
<?php | ||
|
||
namespace Rennokki\Befriended\Traits; | ||
|
||
trait Like | ||
{ | ||
use CanLike, CanBeLiked; | ||
} |
Oops, something went wrong.