I have a Model called Service
which migration is as follows:
Schema::create( 'services', function( Blueprint $table ){
$table->id();
$table->json( 'data' );
$table->dateTime( 'created_at' )->useCurrent();
$table->dateTime( 'updated_at' )->useCurrent()->useCurrentOnUpdate();
$table->dateTime( 'deleted_at' )->nullable();
} );
The idea is to store the information in the Json column, so for example I will have a name, priority, style or an icon. I want to create those attributes for my Service
model using the new Laravel 11 approach:
/**
* Returns the name attribute
*
* @return Attribute
*/
protected function style(): Attribute
{
return Attribute::make(
get: /* Some getter function */,
set: /* Some setter function */
);
}
I would expect to be able to do stuff like:
$s = new Service();
$s->name = 'My service';
$s->save();
Or
$s = Service::find( 1 );
echo $s->name;
I tried implementig the name() function like this:
/**
* The name attribute
*
* @return Attribute
*/
protected function name(): Attribute
{
return Attribute::make(
get: fn () => data_get( $this->data, 'name' ),
set: fn ( string $value ) => data_set( $this->data, 'name', $value )
);
}
I think data_set receives $this->data as & parameter, and that’s the problem. I think I’m very close to solving this, thank you.