Skip to content

Commit 594b3dd

Browse files
lroggenTom Schlick
authored andcommitted
Allow $primaryKey for Pivot (#10)
* tom fixes * readme
1 parent ba04082 commit 594b3dd

File tree

5 files changed

+19
-9
lines changed

5 files changed

+19
-9
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ class PostTagAuditLog extends BaseModel
156156

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

159+
Note:
160+
Both models must use the AuditLoggable trait (ex: Post and Tag) so that `$post->tags()->sync([...])` will work.
161+
159162
### Testing
160163

161164
``` bash

src/Models/BaseModel.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,13 @@ public function recordPivotChanges(int $event_type, $model, string $relationName
106106
*/
107107
public function getPivotChanges($pivot, $model, array $pivotIds) : array
108108
{
109+
$columns = (new $pivot())->getAuditLogForeignKeyColumns();
110+
$key = in_array($model->getForeignKey(), $columns) ? $model->getForeignKey() : $model->getKeyName();
111+
109112
$changes = [];
110-
foreach ((new $pivot())->getAuditLogForeignKeyColumns() as $auditColumn => $pivotColumn) {
111-
foreach ($pivotIds as $id => $pivotId) {
112-
if ($pivotColumn !== $model->getForeignKey()) {
113+
foreach ($pivotIds as $id => $pivotId) {
114+
foreach ($columns as $auditColumn => $pivotColumn) {
115+
if ($pivotColumn !== $key) {
113116
$changes[$id][$auditColumn] = $pivotId;
114117
} else {
115118
$changes[$id][$auditColumn] = $model->getKey();

tests/Fakes/Models/Post.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@ class Post extends Model
1515

1616
protected $guarded = [];
1717

18+
protected $primaryKey = 'post_id';
19+
1820
public function tags()
1921
{
20-
return $this->belongsToMany(Tag::class)
22+
return $this->belongsToMany(Tag::class,
23+
'post_tag',
24+
'post_id',
25+
'tag_id'
26+
)
2127
->using(PostTag::class);
2228
}
2329
}

tests/Fakes/migrations/2019_04_05_101112_add_posts_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class AddPostsTable extends Migration
1414
public function up()
1515
{
1616
Schema::create('posts', function (Blueprint $table) {
17-
$table->increments('id');
17+
$table->increments('post_id');
1818
$table->string('title');
1919
$table->timestamp('posted_at')->index();
2020
$table->timestamps();

tests/PostTagModelTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,12 @@ public function syncing_triggers_a_revision()
103103
public function deleting_a_post_tag_does_not_trigger_a_revision()
104104
{
105105
$tag1 = Tag::create([
106-
'id' => 50,
107106
'title' => 'Here is a comment!',
108107
'posted_at' => '2019-04-05 12:00:00',
109108
]);
110109

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

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

0 commit comments

Comments
 (0)