On a Laravel/Livewire site I try to use dependency injection for defining service class for key/values writing reading.
I defined 2 classes, app/Library/Services/Interfaces/KeyDataInterface.php :
<?php
namespace App\Library\Services\Interfaces;
use App\Models\KeyData;
/**
* Write/read key value into/from storage
*/
interface KeyDataInterface
{
/**
* Write key/value into storage
*
* @param string $key
* @param string $value
* @param int $durationInSeconds
*
* @return KeyData - created model
*/
public static function write( string $key, string $value, int $durationInSeconds = 60 ): KeyData;
}
and implementation in app/Library/Services/KeyDataInDbService.php:
<?php
namespace App\Library\Services;
use App\Library\Services\Interfaces\KeyDataInterface;
use App\Models\KeyData;
use Carbon\Carbon;
/**
* Write/read key value into/from db
*
*/
readonly class KeyDataInDbService implements KeyDataInterface
{
/**
* Write key/value into db
*
* @param string $key
* @param string $value
* @param int $durationInSeconds
*
* @return KeyData - created model
*/
public static function write( string $key, string $value, int $durationInSeconds = 60 ): KeyData {
$keyData = KeyData::getByKey($key)->getByValue($value)->first();
// scopeGetByValue
if(!empty($keyData)) {
$keyData->expired_at = Carbon::now('UTC')
->addSeconds($durationInSeconds);
return $keyData;
}
return KeyData::create([
'key' => $key,
'value' => $value,
'expired_at' => Carbon::now('UTC')->addSeconds($durationInSeconds)
]);
}
}
I tied them in app/Providers/AppServiceProvider.php :
public function register(): void
{
$this->app->bind(
'App\Library\Services\Interfaces\KeyDataInterface',
'App\Library\Services\KeyDataInDbService'
);
}
But when I tried to use is in Livewire component :
class TheTaskIsViewed extends Component
{
...
public KeyDataInterface $keyDataService;
public function boot(KeyDataInterface $keyDataService)
{
$this->keyDataService = $keyDataService;
}
public function mount(int $taskId)
{
I got error:
Property type not supported in Livewire for property: [{}]
In browser I see pointing to 12 line of a file which has only 8 lines:
Component TheTaskIsViewed is used on resources/views/livewire/test.blade.php as :
<div class="admin_page_container" id="app_image_admin_page_container pt-64" x-cloak>
<fieldset class="p-1 m-1 mb-6 bordered">
<legend class="bordered">
the-task-is-viewed
</legend>
<livewire:admin.the-task-is-viewed taskId="5"/>
</fieldset>
</div>
Why have I got this error and how it can be fixed ?
"laravel/framework": "^10.48.4",
"livewire/livewire": "^3.4.9",
In Livewire's public properties, Only some types are supported. https://livewire.laravel.com/docs/properties#supported-property-types
If you don't use $keyDataService
in the view, make it private or protected.
protected KeyDataInterface $keyDataService;
If you want to use $keyDataService
in views, create a custom type.