Skip to content

Commit

Permalink
Allow $primaryKey for Pivot (#10)
Browse files Browse the repository at this point in the history
* tom fixes

* readme
  • Loading branch information
lroggen authored and Tom Schlick committed Aug 29, 2019
1 parent ba04082 commit 594b3dd
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ class PostTagAuditLog extends BaseModel

For a working example of pivots with the audit log, see `laravel-model-auditlog/tests/Fakes`, which contains working migrations and models.

Note:
Both models must use the AuditLoggable trait (ex: Post and Tag) so that `$post->tags()->sync([...])` will work.

### Testing

``` bash
Expand Down
9 changes: 6 additions & 3 deletions src/Models/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,13 @@ public function recordPivotChanges(int $event_type, $model, string $relationName
*/
public function getPivotChanges($pivot, $model, array $pivotIds) : array
{
$columns = (new $pivot())->getAuditLogForeignKeyColumns();
$key = in_array($model->getForeignKey(), $columns) ? $model->getForeignKey() : $model->getKeyName();

$changes = [];
foreach ((new $pivot())->getAuditLogForeignKeyColumns() as $auditColumn => $pivotColumn) {
foreach ($pivotIds as $id => $pivotId) {
if ($pivotColumn !== $model->getForeignKey()) {
foreach ($pivotIds as $id => $pivotId) {
foreach ($columns as $auditColumn => $pivotColumn) {
if ($pivotColumn !== $key) {
$changes[$id][$auditColumn] = $pivotId;
} else {
$changes[$id][$auditColumn] = $model->getKey();
Expand Down
8 changes: 7 additions & 1 deletion tests/Fakes/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ class Post extends Model

protected $guarded = [];

protected $primaryKey = 'post_id';

public function tags()
{
return $this->belongsToMany(Tag::class)
return $this->belongsToMany(Tag::class,
'post_tag',
'post_id',
'tag_id'
)
->using(PostTag::class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AddPostsTable extends Migration
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->increments('post_id');
$table->string('title');
$table->timestamp('posted_at')->index();
$table->timestamps();
Expand Down
6 changes: 2 additions & 4 deletions tests/PostTagModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,12 @@ public function syncing_triggers_a_revision()
public function deleting_a_post_tag_does_not_trigger_a_revision()
{
$tag1 = Tag::create([
'id' => 50,
'title' => 'Here is a comment!',
'posted_at' => '2019-04-05 12:00:00',
]);

/** @var Post $post */
$post = Post::create([
'id' => 2000,
'title' => 'Test',
'posted_at' => '2019-04-05 12:00:00',
]);
Expand All @@ -122,10 +120,10 @@ public function deleting_a_post_tag_does_not_trigger_a_revision()
//hasMany relationship works on tag model to audit log
$this->assertEquals(2, $tag1->auditLogs()->count());
//Record correct in pivot
$this->assertEquals(1, PostTag::where('post_id', 2000)->where('tag_id', 50)->count());

$this->assertEquals(1, PostTag::where('post_id', 1)->where('tag_id', 1)->count());

$tag2 = Tag::create([
'id' => 99,
'title' => 'Here is another comment!',
'posted_at' => '2019-04-06 12:00:00',
]);
Expand Down

0 comments on commit 594b3dd

Please sign in to comment.