Skip to content

Message User Relationship #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});