spatie / laravel-translatable

Making Eloquent models translatable

Home Page:https://spatie.be/docs/laravel-translatable

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Return value of App\Models\Foobar::getLocale() must be of the type string, null returned

f-liva opened this issue · comments

How it is possible this error?

Symfony\Component\Debug\Exception\FatalThrowableError: Return value of App\Models\Product::getLocale() must be of the type string, null returned in /home/forge/vivi.city/vendor/spatie/laravel-translatable/src/HasTranslations.php:183
Stack trace:
#0 /home/forge/vivi.city/vendor/spatie/laravel-translatable/src/HasTranslations.php(30): App\Models\Product->getLocale()
#1 /home/forge/vivi.city/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(329): App\Models\Product->setAttribute('name', 'Frank Skinner -...')
#2 /home/forge/vivi.city/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(436): Illuminate\Database\Eloquent\Model->fill(Array)
#3 /home/forge/vivi.city/vendor/laravel/framework/src/Illuminate/Support/helpers.php(1124): Illuminate\Database\Eloquent\Builder->Illuminate\Database\Eloquent\{closure}(Object(App\Models\Product))
#4 /home/forge/vivi.city/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(437): tap(Object(App\Models\Product), Object(Closure))
#5 /home/forge/vivi.city/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php(23): Illuminate\Database\Eloquent\Builder->updateOrCreate(Array, Array)
#6 /home/forge/vivi.city/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1618): Illuminate\Database\Eloquent\Model->forwardCallTo(Object(Fico7489\Laravel\EloquentJoin\EloquentJoinBuilder), 'updateOrCreate', Array)
#7 /home/forge/vivi.city/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1630): Illuminate\Database\Eloquent\Model->__call('updateOrCreate', Array)
#8 /home/forge/vivi.city/app/Jobs/FetchProduct.php(80): Illuminate\Database\Eloquent\Model::__callStatic('updateOrCreate', Array)
#9 /home/forge/vivi.city/app/Jobs/Encore/FetchProduct.php(19): App\Jobs\FetchProduct->process()
#10 /home/forge/vivi.city/app/Jobs/FetchProduct.php(64): App\Jobs\Encore\FetchProduct->process()
#11 [internal function]: App\Jobs\FetchProduct->handle()
#12 /home/forge/vivi.city/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(32): call_user_func_array(Array, Array)
#13 /home/forge/vivi.city/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(90): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#14 /home/forge/vivi.city/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(34): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#15 /home/forge/vivi.city/vendor/laravel/framework/src/Illuminate/Container/Container.php(576): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#16 /home/forge/vivi.city/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(94): Illuminate\Container\Container->call(Array)
#17 /home/forge/vivi.city/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\Bus\Dispatcher->Illuminate\Bus\{closure}(Object(App\Jobs\Encore\FetchProduct))
#18 /home/forge/vivi.city/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(104): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(App\Jobs\Encore\FetchProduct))
#19 /home/forge/vivi.city/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(98): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#20 /home/forge/vivi.city/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(49): Illuminate\Bus\Dispatcher->dispatchNow(Object(App\Jobs\Encore\FetchProduct), false)
#21 /home/forge/vivi.city/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(88): Illuminate\Queue\CallQueuedHandler->call(Object(Illuminate\Queue\Jobs\RedisJob), Array)
#22 /home/forge/vivi.city/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(337): Illuminate\Queue\Jobs\Job->fire()
#23 /home/forge/vivi.city/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(283): Illuminate\Queue\Worker->process('redis', Object(Illuminate\Queue\Jobs\RedisJob), Object(Illuminate\Queue\WorkerOptions))
#24 /home/forge/vivi.city/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(118): Illuminate\Queue\Worker->runJob(Object(Illuminate\Queue\Jobs\RedisJob), 'redis', Object(Illuminate\Queue\WorkerOptions))
#25 /home/forge/vivi.city/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(102): Illuminate\Queue\Worker->daemon('redis', 'encore', Object(Illuminate\Queue\WorkerOptions))
#26 /home/forge/vivi.city/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(86): Illuminate\Queue\Console\WorkCommand->runWorker('redis', 'encore')
#27 /home/forge/vivi.city/vendor/laravel/horizon/src/Console/WorkCommand.php(46): Illuminate\Queue\Console\WorkCommand->handle()
#28 [internal function]: Laravel\Horizon\Console\WorkCommand->handle()
#29 /home/forge/vivi.city/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(32): call_user_func_array(Array, Array)
#30 /home/forge/vivi.city/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(90): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#31 /home/forge/vivi.city/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(34): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#32 /home/forge/vivi.city/vendor/laravel/framework/src/Illuminate/Container/Container.php(576): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#33 /home/forge/vivi.city/vendor/laravel/framework/src/Illuminate/Console/Command.php(183): Illuminate\Container\Container->call(Array)
#34 /home/forge/vivi.city/vendor/symfony/console/Command/Command.php(255): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#35 /home/forge/vivi.city/vendor/laravel/framework/src/Illuminate/Console/Command.php(170): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#36 /home/forge/vivi.city/vendor/symfony/console/Application.php(915): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#37 /home/forge/vivi.city/vendor/symfony/console/Application.php(272): Symfony\Component\Console\Application->doRunCommand(Object(Laravel\Horizon\Console\WorkCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#38 /home/forge/vivi.city/vendor/symfony/console/Application.php(148): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#39 /home/forge/vivi.city/vendor/laravel/framework/src/Illuminate/Console/Application.php(90): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#40 /home/forge/vivi.city/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(133): Illuminate\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#41 /home/forge/vivi.city/artisan(37): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#42 {main}

Try running config('app.locale') in a tinker session. Does it return the locale you are expecting?

Yes, from tinx returns "it" which corresponds to my default language.

Why have you already closed the issue?

I can’t reproduce this. Could you try debugging this by adding some dump statements in HasTranslations?

Dear contributor,

because this issue seems to be inactive for quite some time now, I've automatically closed it. If you feel this issue deserves some attention from my human colleagues feel free to reopen it.

for future reference:
I had the same issue, this was caused in my case because middleware we use to set a locale based on a user “language” column in the database. But when there is no user language (in tests) the language got set to null

for future reference: I had the same issue, this was caused in my case because middleware we use to set a locale based on a user “language” column in the database. But when there is no user language (in tests) the language got set to null

You add just add a default local to the session in SetLocale Middleware like this

$locale = session()->get('locale', 'ar');