Skip to content

Commit 687b851

Browse files
committed
add ticket categories api
1 parent 33cdfa8 commit 687b851

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

src/Concerns/InteractsWithTicketRelations.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,29 @@ public function syncLabels($ids, $detaching = true)
2828
{
2929
return $this->labels()->sync($ids, $detaching);
3030
}
31+
32+
/**
33+
* Associate Categories into an existing ticket
34+
*
35+
* @param mixed $id
36+
* @param array $attributes
37+
* @param bool $touch
38+
* @return void
39+
*/
40+
public function attachCategories($id, array $attributes = [], $touch = true)
41+
{
42+
$this->categories()->attach($id, $attributes, $touch);
43+
}
44+
45+
/**
46+
* Sync the intermediate tables with a list of IDs or collection of the ticket model..
47+
*
48+
* @param \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array $ids
49+
* @param bool $detaching
50+
* @return array
51+
*/
52+
public function syncCategories($ids, $detaching = true)
53+
{
54+
return $this->categories()->sync($ids, $detaching);
55+
}
3156
}

src/Models/Ticket.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,14 @@ public function comments(): HasMany
6464
*/
6565
public function categories(): BelongsToMany
6666
{
67-
return $this->belongsToMany(Category::class, 'categorizable');
67+
$table = config('laravel_ticket.table_names.category_ticket', 'category_ticket');
68+
69+
return $this->belongsToMany(
70+
Category::class,
71+
$table['table'],
72+
$table['columns']['ticket_foreign_id'],
73+
$table['columns']['category_foreign_id'],
74+
);
6875
}
6976

7077
/**

tests/Feature/TicketRelationsTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Coderflex\LaravelTicket\Models\Category;
34
use Coderflex\LaravelTicket\Models\Label;
45
use Coderflex\LaravelTicket\Models\Ticket;
56
use Coderflex\LaravelTicket\Tests\Models\User;
@@ -45,3 +46,34 @@
4546

4647
$this->assertEquals($ticket->labels->count(), 5);
4748
});
49+
50+
it('associates categories to a ticket', function () {
51+
$categories = Category::factory()->times(3)->create();
52+
$ticket = Ticket::factory()->create();
53+
54+
$ticket->attachCategories($categories->pluck('id'));
55+
56+
$this->assertEquals($ticket->categories->count(), 3);
57+
});
58+
59+
it('sync categories to a ticket', function () {
60+
$categories = Category::factory()->times(2)->create();
61+
$ticket = Ticket::factory()->create();
62+
63+
$ticket->syncCategories($categories->pluck('id'));
64+
65+
$this->assertEquals($ticket->categories->count(), 2);
66+
});
67+
68+
it('sync categories to a ticket without detaching', function () {
69+
$categories = Category::factory()->times(3)->create();
70+
$ticket = Ticket::factory()->create();
71+
$ticket->attachCategories($categories->pluck('id'));
72+
73+
$anotherCategories = Category::factory()->times(2)->create();
74+
75+
$ticket->syncCategories($anotherCategories->pluck('id'), false);
76+
77+
$this->assertEquals($ticket->categories->count(), 5);
78+
});
79+

0 commit comments

Comments
 (0)