File tree Expand file tree Collapse file tree 4 files changed +30
-1
lines changed Expand file tree Collapse file tree 4 files changed +30
-1
lines changed Original file line number Diff line number Diff line change 3939 * @see https://laravel.com/docs/9.x/eloquent-relationships#one-to-many
4040 */
4141 'columns ' => [
42+ 'user_foreing_id ' => 'user_id ' ,
4243 'ticket_foreing_id ' => 'ticket_id ' ,
4344 ],
4445 ],
Original file line number Diff line number Diff line change @@ -15,7 +15,7 @@ return new class extends Migration
1515
1616 Schema::create($tableName['table'], function (Blueprint $table) use ($tableName) {
1717 $table->id();
18- $table->foreignId('user_id' );
18+ $table->foreignId($tableName['columns']['user_foreing_id'] );
1919 $table->foreignId($tableName['columns']['ticket_foreing_id']);
2020 $table->text('message');
2121 $table->timestamps();
Original file line number Diff line number Diff line change @@ -38,6 +38,21 @@ public function ticket(): BelongsTo
3838 );
3939 }
4040
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+
4156 /**
4257 * Get the table associated with the model.
4358 *
Original file line number Diff line number Diff line change 22
33use Coderflex \LaravelTicket \Models \Message ;
44use Coderflex \LaravelTicket \Models \Ticket ;
5+ use Coderflex \LaravelTicket \Tests \Models \User ;
56
67it ('can attach message to a ticket ' , function () {
78 $ message = Message::factory ()->create ();
1314
1415 $ this ->assertEquals ($ message ->ticket ->title , 'Can you create a message? ' );
1516});
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