Skip to content

Commit

Permalink
Merge pull request #16 from coderflexx/user-messages-relationship
Browse files Browse the repository at this point in the history
Message User Relationship
  • Loading branch information
ousid authored Dec 25, 2022
2 parents 779999d + 238c3fe commit 006499a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions config/laravel_ticket.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
* @see https://laravel.com/docs/9.x/eloquent-relationships#one-to-many
*/
'columns' => [
'user_foreing_id' => 'user_id',
'ticket_foreing_id' => 'ticket_id',
],
],
Expand Down
2 changes: 1 addition & 1 deletion database/migrations/create_messages_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ return new class extends Migration

Schema::create($tableName['table'], function (Blueprint $table) use ($tableName) {
$table->id();
$table->foreignId('user_id');
$table->foreignId($tableName['columns']['user_foreing_id']);
$table->foreignId($tableName['columns']['ticket_foreing_id']);
$table->text('message');
$table->timestamps();
Expand Down
15 changes: 15 additions & 0 deletions src/Models/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ public function ticket(): BelongsTo
);
}

/**
* Get Message Relationship
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user(): BelongsTo
{
$tableName = config('laravel_ticket.table_names.messages', 'message');

return $this->belongsTo(
config('auth.providers.users.model'),
$tableName['columns']['user_foreing_id']
);
}

/**
* Get the table associated with the model.
*
Expand Down
13 changes: 13 additions & 0 deletions tests/Feature/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Coderflex\LaravelTicket\Models\Message;
use Coderflex\LaravelTicket\Models\Ticket;
use Coderflex\LaravelTicket\Tests\Models\User;

it('can attach message to a ticket', function () {
$message = Message::factory()->create();
Expand All @@ -13,3 +14,15 @@

$this->assertEquals($message->ticket->title, 'Can you create a message?');
});

it('message can be associated to a user', function () {
$user = User::factory()->create([
'name' => 'Oussama',
]);

$message = Message::factory()->create();

$message->user()->associate($user);

$this->assertEquals($message->user->name, 'Oussama');
});

0 comments on commit 006499a

Please sign in to comment.