File tree 4 files changed +30
-1
lines changed
4 files changed +30
-1
lines changed Original file line number Diff line number Diff line change 39
39
* @see https://laravel.com/docs/9.x/eloquent-relationships#one-to-many
40
40
*/
41
41
'columns ' => [
42
+ 'user_foreing_id ' => 'user_id ' ,
42
43
'ticket_foreing_id ' => 'ticket_id ' ,
43
44
],
44
45
],
Original file line number Diff line number Diff line change @@ -15,7 +15,7 @@ return new class extends Migration
15
15
16
16
Schema::create($tableName['table'], function (Blueprint $table) use ($tableName) {
17
17
$table->id();
18
- $table->foreignId('user_id' );
18
+ $table->foreignId($tableName['columns']['user_foreing_id'] );
19
19
$table->foreignId($tableName['columns']['ticket_foreing_id']);
20
20
$table->text('message');
21
21
$table->timestamps();
Original file line number Diff line number Diff line change @@ -38,6 +38,21 @@ public function ticket(): BelongsTo
38
38
);
39
39
}
40
40
41
+ /**
42
+ * Get Message Relationship
43
+ *
44
+ * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
45
+ */
46
+ public function user (): BelongsTo
47
+ {
48
+ $ tableName = config ('laravel_ticket.table_names.messages ' , 'message ' );
49
+
50
+ return $ this ->belongsTo (
51
+ config ('auth.providers.users.model ' ),
52
+ $ tableName ['columns ' ]['user_foreing_id ' ]
53
+ );
54
+ }
55
+
41
56
/**
42
57
* Get the table associated with the model.
43
58
*
Original file line number Diff line number Diff line change 2
2
3
3
use Coderflex \LaravelTicket \Models \Message ;
4
4
use Coderflex \LaravelTicket \Models \Ticket ;
5
+ use Coderflex \LaravelTicket \Tests \Models \User ;
5
6
6
7
it ('can attach message to a ticket ' , function () {
7
8
$ message = Message::factory ()->create ();
13
14
14
15
$ this ->assertEquals ($ message ->ticket ->title , 'Can you create a message? ' );
15
16
});
17
+
18
+ it ('message can be associated to a user ' , function () {
19
+ $ user = User::factory ()->create ([
20
+ 'name ' => 'Oussama ' ,
21
+ ]);
22
+
23
+ $ message = Message::factory ()->create ();
24
+
25
+ $ message ->user ()->associate ($ user );
26
+
27
+ $ this ->assertEquals ($ message ->user ->name , 'Oussama ' );
28
+ });
You can’t perform that action at this time.
0 commit comments