ARCANEDEV/LaravelMessenger

Error with eager loading

Closed this issue · 2 comments

  • LaravelMessenger Version: ^7.1
  • Laravel Version: 6.5
  • PHP Version: 7.3

Description:

When I try to consult a discussion it brings me the discussion with the wrong id. That is to say I only have one discussion in the database, and when the query tells me that the id is 2, also when I add with('messages') to bring all the messages all right, but when I add the scope forUser does not bring me any message.

Steps To Reproduce:

use Arcanedev\LaravelMessenger\Models\Discussion as Thread;

/**
 * Show all of the message threads to the user.
 *
 * @return mixed
 */
public function index()
{
    $user = Auth::user();
    $threads = Thread::with('messages')->forUser($user)->paginate(15);

    return response()->json($threads);
}

Response

{
    "current_page": 1,
    "data": [
        {
            "id": 2,
            "subject": "First Message",
            "created_at": "2019-11-14 20:58:15",
            "updated_at": "2019-11-14 20:58:15",
            "deleted_at": null,
            "discussion_id": 1,
            "participable_type": "App\\User",
            "participable_id": 1,
            "last_read": null,
            "messages": []
        }
    ],
    "first_page_url": "url.com/api/messages?page=1",
    "from": 1,
    "last_page": 1,
    "last_page_url": "url.com/api/messages?page=1",
    "next_page_url": null,
    "path": "url.com/api/messages",
    "per_page": 15,
    "prev_page_url": null,
    "to": 1,
    "total": 1
}

Are you sure you're using the correct package ?

The response isn't similar to what our package offer.

Try to use these steps and see what you got:

1- Install our package with a fresh laravel installation

  • Setup the database (try with sqlite)

2- Add a seeder

<?php

// database/seeds/MessengerSeeder.php

declare(strict_types=1);

use App\User;
use Arcanedev\LaravelMessenger\Models\Discussion;
use Arcanedev\LaravelMessenger\Models\Message;
use Illuminate\Database\Seeder;

class MessengerSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        /**
         * @var User $john
         * @var User $jane
         * @var User $stranger
         */
        $john     = factory(User::class)->create(['id' => 1, 'name' => 'John DOE']);
        $jane     = factory(User::class)->create(['id' => 2, 'name' => 'Jane DOE']);
        $stranger = factory(User::class)->create(['id' => 3, 'name' => 'xX_ZONDA_KILLER69_Xx']);

        // CREATING DISCUSSION #1
        tap(
            Discussion::query()->create(['subject' => 'Welcome to the public chat']),
            function (Discussion $discussion) use ($jane, $john, $stranger) {
                /* Participants */
                $discussion->addParticipants([$john, $jane, $stranger]);

                /* Messages */
                $discussion->messages()->saveMany([
                    $this->makeMessage($stranger, 'First !!!'),
                    $this->makeMessage($john, 'Nooo, i was too late :('),
                    $this->makeMessage($jane, 'Heyoooooo ( ^_^)b'),
                ]);
            }
        );

        // CREATING DISCUSSION #2
        tap(
            Discussion::query()->create(['subject' => 'Private messages between John & Jane']),
            function (Discussion $discussion) use ($jane, $john) {
                /* Participants */
                $discussion->addParticipants([$john, $jane]);

                /* Messages */
                $discussion->messages()->saveMany([
                    $this->makeMessage($john, 'Hi jane! Long time no see :)'),
                    $this->makeMessage($jane, 'Wadup!'),
                ]);
            }
        );
    }

    /**
     * Make a new message.
     *
     * @param  \Illuminate\Database\Eloquent\Model|mixed  $participant
     * @param  string                                     $body
     *
     * @return \Arcanedev\LaravelMessenger\Models\Message
     */
    protected function makeMessage($participant, $body)
    {
        return new Message([
            'participable_type' => $participant->getMorphClass(),
            'participable_id'   => $participant->getKey(),
            'body'              => $body,
        ]);
    }
}

Edit the DatabaseSeeder class.

<?php
// database/seeds/MessengerSeeder.php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        // $this->call(UsersTableSeeder::class);

        $this->call(MessengerSeeder::class);
    }
}

3- Run migrations with seeds

php artisan migrate:fresh --seed

4- Routes

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
use Arcanedev\LaravelMessenger\Models\Discussion;

Route::get('disscussions/{user_id}', function (int $userId) {
    /** @var \App\User $user */
    $user = Auth::loginUsingId($userId);

    return Discussion::forUser($user)
        ->with(['messages.participable'])
        ->paginate(15);
});

5- Testing

You can test the package by visiting http://your-project/disscussions/{user_id}, user_id is in [1, 2, 3] (3 users).

This is what the response looks like when you visit http://your-project/disscussions/1

{
    "current_page": 1,
    "data": [
        {
            "id": 1,
            "subject": "Welcome to the public chat",
            "created_at": "2020-02-07 11:43:13",
            "updated_at": "2020-02-07 11:43:17",
            "deleted_at": null,
            "messages": [
                {
                    "id": 1,
                    "discussion_id": 1,
                    "participable_type": "App\\User",
                    "participable_id": 3,
                    "body": "First !!!",
                    "created_at": "2020-02-07 11:43:15",
                    "updated_at": "2020-02-07 11:43:15",
                    "deleted_at": null,
                    "participable": {
                        "id": 3,
                        "name": "xX_ZONDA_KILLER69_Xx",
                        "email": "cohara@example.net",
                        "email_verified_at": "2020-02-07 11:43:13",
                        "created_at": "2020-02-07 11:43:13",
                        "updated_at": "2020-02-07 11:43:13"
                    }
                },
                {
                    "id": 2,
                    "discussion_id": 1,
                    "participable_type": "App\\User",
                    "participable_id": 1,
                    "body": "Nooo, i was too late :(",
                    "created_at": "2020-02-07 11:43:16",
                    "updated_at": "2020-02-07 11:43:16",
                    "deleted_at": null,
                    "participable": {
                        "id": 1,
                        "name": "John DOE",
                        "email": "wehner.kacey@example.com",
                        "email_verified_at": "2020-02-07 11:43:12",
                        "created_at": "2020-02-07 11:43:12",
                        "updated_at": "2020-02-07 11:43:12"
                    }
                },
                {
                    "id": 3,
                    "discussion_id": 1,
                    "participable_type": "App\\User",
                    "participable_id": 2,
                    "body": "Heyoooooo ( ^_^)b",
                    "created_at": "2020-02-07 11:43:16",
                    "updated_at": "2020-02-07 11:43:16",
                    "deleted_at": null,
                    "participable": {
                        "id": 2,
                        "name": "Jane DOE",
                        "email": "marge33@example.net",
                        "email_verified_at": "2020-02-07 11:43:12",
                        "created_at": "2020-02-07 11:43:12",
                        "updated_at": "2020-02-07 11:43:12"
                    }
                }
            ]
        },
        {
            "id": 2,
            "subject": "Private messages between John & Jane",
            "created_at": "2020-02-07 11:43:17",
            "updated_at": "2020-02-07 11:43:19",
            "deleted_at": null,
            "messages": [
                {
                    "id": 4,
                    "discussion_id": 2,
                    "participable_type": "App\\User",
                    "participable_id": 1,
                    "body": "Hi jane! Long time no see :)",
                    "created_at": "2020-02-07 11:43:18",
                    "updated_at": "2020-02-07 11:43:18",
                    "deleted_at": null,
                    "participable": {
                        "id": 1,
                        "name": "John DOE",
                        "email": "wehner.kacey@example.com",
                        "email_verified_at": "2020-02-07 11:43:12",
                        "created_at": "2020-02-07 11:43:12",
                        "updated_at": "2020-02-07 11:43:12"
                    }
                },
                {
                    "id": 5,
                    "discussion_id": 2,
                    "participable_type": "App\\User",
                    "participable_id": 2,
                    "body": "Wadup!",
                    "created_at": "2020-02-07 11:43:19",
                    "updated_at": "2020-02-07 11:43:19",
                    "deleted_at": null,
                    "participable": {
                        "id": 2,
                        "name": "Jane DOE",
                        "email": "marge33@example.net",
                        "email_verified_at": "2020-02-07 11:43:12",
                        "created_at": "2020-02-07 11:43:12",
                        "updated_at": "2020-02-07 11:43:12"
                    }
                }
            ]
        }
    ],
    "first_page_url": "http://your-project/disscussions/1?page=1",
    "from": 1,
    "last_page": 1,
    "last_page_url": "http://your-project/disscussions/1?page=1",
    "next_page_url": null,
    "path": "http://your-project/disscussions/1",
    "per_page": 15,
    "prev_page_url": null,
    "to": 2,
    "total": 2
}

Thanks for the answer, but I had to stop using this package because I could not fix the error at the time.