Skip to content

Add user relation to message model #10

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

Closed
wants to merge 2 commits into from

Conversation

krekas
Copy link
Contributor

@krekas krekas commented Nov 10, 2022

When displaying messages to show who wrote message I need user relation in message model

*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please follow the pattern like the rest of the methods? Like the ticket() method, and adding the foreign key to the config file under messages key will be appreciated!

If you can add few tests for the user() method under MessageTest file, will be perfect, for future changes.

@krekas
Copy link
Contributor Author

krekas commented Nov 11, 2022

@ousid added foreign key to config, but sorry don't know how to add test when package doesn't have User model in Laravels default location.

@ousid
Copy link
Member

ousid commented Nov 12, 2022

Thanks, @krekas

There's a default user model, for tests purposes, here, you can use Coderflex\LaravelTicket\Tests\Models\User namespace for the User model.

Here's an example

@krekas
Copy link
Contributor Author

krekas commented Nov 12, 2022

@ousid the problem for me is in Message model with User relation. Let's say I have test like this

it('can retrieve user from message', function () {
    $user = User::factory()->create();

    $ticket = Ticket::factory()->create();

    $tableName = config(
        'laravel_ticket.table_names.messages',
        'messages'
    );

    $message = Message::factory()
        ->create([
            $tableName['columns']['ticket_foreing_id'] => $ticket->id,
            $tableName['columns']['user_foreign_id']   => $user->id,
        ]);

    $this->assertDatabaseHas($tableName['table'], [
        $tableName['columns']['user_foreign_id'] => $user->id,
    ]);

    $this->assertEquals($ticket->messages->count(), 1);
    $this->assertEquals($message->user->id, $user->id);
});

And I get error Error : Class "App\Models\User" not found because well relation is belongs to user and here User model is from App\Models\User

public function user(): BelongsTo
{
    $tableName = config('laravel_ticket.table_names.messages', 'messages');

    return $this->belongsTo(
        User::class,
        $tableName['columns']['user_foreign_id']
    );
}

@ousid
Copy link
Member

ousid commented Dec 25, 2022

Closing this PR, since it does not meet the requirements, and replaced with #16

You may want to take a look at it.

Thanks for your contribution @krekas!

@ousid ousid closed this Dec 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants