cmgmyr / laravel-messenger

Simple user messaging package for Laravel
MIT License
2.46k stars 517 forks source link

Can't select Message::unreadForUser #356

Closed RomainMazB closed 3 years ago

RomainMazB commented 3 years ago

Hi, I faced this issue using Message::unreadForUser:

Facade\Ignition\Exceptions\ViewException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'messenger_participants.message_id' in 'where clause' (SQL: select * from `messenger_messages` where exists (select * from `messenger_threads` where `messenger_messages`.`thread_id` = `messenger_threads`.`id` and `messenger_threads`.`deleted_at` is null) and `user_id` != ad684da1-43ad-4b24-9064-bea26b14bbcb and exists (select * from `messenger_participants` where `messenger_messages`.`thread_id` = `messenger_participants`.`message_id` and `user_id` = ad684da1-43ad-4b24-9064-bea26b14bbcb and (`last_read` < messenger_messages.created_at or `last_read` is null) and `messenger_participants`.`deleted_at` is null) and `messenger_messages`.`deleted_at` is null) (View: /home/maz/websites/terrajdr/resources/views/dashboard.blade.php) in file /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 678

#0 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Database/Connection.php(638): Illuminate\Database\Connection->runQueryCallback()
#1 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Database/Connection.php(346): Illuminate\Database\Connection->run()
#2 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(2313): Illuminate\Database\Connection->select()
#3 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(2301): Illuminate\Database\Query\Builder->runSelect()
#4 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(2796): Illuminate\Database\Query\Builder->Illuminate\Database\Query\{closure}()
#5 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(2302): Illuminate\Database\Query\Builder->onceWithColumns()
#6 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(571): Illuminate\Database\Query\Builder->get()
#7 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(555): Illuminate\Database\Eloquent\Builder->getModels()
#8 /home/maz/websites/terrajdr/vendor/cmgmyr/messenger/src/Traits/Messagable.php(71): Illuminate\Database\Eloquent\Builder->get()
#9 /home/maz/websites/terrajdr/app/View/Components/AppLayout.php(22): App\Models\User->unreadMessagesCount()
#10 [internal function]: App\View\Components\AppLayout->__construct()
#11 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Container/Container.php(870): ReflectionClass->newInstanceArgs()
#12 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Container/Container.php(712): Illuminate\Container\Container->build()
#13 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(826): Illuminate\Container\Container->resolve()
#14 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Container/Container.php(651): Illuminate\Foundation\Application->resolve()
#15 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(811): Illuminate\Container\Container->make()
#16 /home/maz/websites/terrajdr/resources/views/dashboard.blade.php(2): Illuminate\Foundation\Application->make()
#17 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php(107): require('/home/maz/websi...')
#18 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php(108): Illuminate\Filesystem\Filesystem::Illuminate\Filesystem\{closure}()
#19 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/View/Engines/PhpEngine.php(58): Illuminate\Filesystem\Filesystem->getRequire()
#20 /home/maz/websites/terrajdr/vendor/livewire/livewire/src/ComponentConcerns/RendersLivewireComponents.php(37): Illuminate\View\Engines\PhpEngine->evaluatePath()
#21 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/View/Engines/CompilerEngine.php(61): Livewire\CompilerEngineForIgnition->evaluatePath()
#22 /home/maz/websites/terrajdr/vendor/facade/ignition/src/Views/Engines/CompilerEngine.php(37): Illuminate\View\Engines\CompilerEngine->get()
#23 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/View/View.php(139): Facade\Ignition\Views\Engines\CompilerEngine->get()
#24 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/View/View.php(122): Illuminate\View\View->getContents()
#25 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/View/View.php(91): Illuminate\View\View->renderContents()
#26 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Http/Response.php(62): Illuminate\View\View->render()
#27 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Http/Response.php(34): Illuminate\Http\Response->setContent()
#28 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Routing/Router.php(783): Illuminate\Http\Response->__construct()
#29 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Routing/Router.php(753): Illuminate\Routing\Router::toResponse()
#30 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Routing/Router.php(693): Illuminate\Routing\Router->prepareResponse()
#31 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\Routing\Router->Illuminate\Routing\{closure}()
#32 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Auth/Middleware/EnsureEmailIsVerified.php(30): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#33 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Auth\Middleware\EnsureEmailIsVerified->handle()
#34 /home/maz/websites/terrajdr/vendor/laravel/passport/src/Http/Middleware/CreateFreshApiToken.php(50): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#35 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Laravel\Passport\Http\Middleware\CreateFreshApiToken->handle()
#36 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(50): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#37 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Routing\Middleware\SubstituteBindings->handle()
#38 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php(44): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#39 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Auth\Middleware\Authenticate->handle()
#40 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php(78): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#41 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle()
#42 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(49): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#43 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\View\Middleware\ShareErrorsFromSession->handle()
#44 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Session/Middleware/AuthenticateSession.php(58): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#45 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Session\Middleware\AuthenticateSession->handle()
#46 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(121): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#47 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(63): Illuminate\Session\Middleware\StartSession->handleStatefulRequest()
#48 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Session\Middleware\StartSession->handle()
#49 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(37): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#50 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle()
#51 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(67): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#52 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Cookie\Middleware\EncryptCookies->handle()
#53 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#54 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Routing/Router.php(695): Illuminate\Pipeline\Pipeline->then()
#55 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Routing/Router.php(670): Illuminate\Routing\Router->runRouteWithinStack()
#56 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Routing/Router.php(636): Illuminate\Routing\Router->runRoute()
#57 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Routing/Router.php(625): Illuminate\Routing\Router->dispatchToRoute()
#58 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(166): Illuminate\Routing\Router->dispatch()
#59 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}()
#60 /home/maz/websites/terrajdr/vendor/spatie/laravel-csp/src/AddCspHeaders.php(13): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#61 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Spatie\Csp\AddCspHeaders->handle()
#62 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#63 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle()
#64 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#65 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle()
#66 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#67 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle()
#68 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php(86): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#69 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance->handle()
#70 /home/maz/websites/terrajdr/vendor/fruitcake/laravel-cors/src/HandleCors.php(37): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#71 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Fruitcake\Cors\HandleCors->handle()
#72 /home/maz/websites/terrajdr/vendor/fideloper/proxy/src/TrustProxies.php(57): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#73 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Fideloper\Proxy\TrustProxies->handle()
#74 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#75 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(141): Illuminate\Pipeline\Pipeline->then()
#76 /home/maz/websites/terrajdr/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(110): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter()
#77 /home/maz/websites/terrajdr/public/index.php(54): Illuminate\Foundation\Http\Kernel->handle()
#78 /home/maz/.config/composer/vendor/cpriego/valet-linux/server.php(232): require('/home/maz/websi...')
#79 {main}

The issue is logical as there is no direct relation between the message and the thread's participants (expect for the user who created the message) but I'm surprised that nobody discover this issue before 🤔

I already have a fix for this issue and I'll create a PR immediately but could someone test this feature? The tests only access the new message through the $user->threadsWithNewMessages() method which is slightly different as it return the threads count and not the messages count which could be different if many message in one thread are unread.

cmgmyr commented 3 years ago

Closing, see comment in https://github.com/cmgmyr/laravel-messenger/pull/357#issuecomment-832222741