Coder Social home page Coder Social logo

Comments (4)

koomai avatar koomai commented on August 29, 2024 1
$data = json_decode($json, true);

$data['animal']['class']::from($data['animal']);

from laravel-data.

verner-lall avatar verner-lall commented on August 29, 2024

I am curious why even use resolve for that? Can you not use ContactData::from($yourData) or ContactData::createAndValidate($yourData) as documentation suggests?

from laravel-data.

abdevcode avatar abdevcode commented on August 29, 2024

Let me give an example of what I am doing.
Imagine that I have a house and I have animals (different type of animals with different properties)
The code would be something like this

interface Animal {}

class DogData extends Data implements Animal
{
    public string $class = self::class;

    public function __construct(
        public string $name,
        public string $color,
    ){}
}

class BirdData extends Data implements Animal
{
    public string $class = self::class;

    public function __construct(
        public string $name,
        public float $weight,
    ){}
}

class HouseData extends Data implements Animal
{
    public function __construct(
        public Animal $animal,
    ){}
}

So now I can have one animal in my house, doing this

        $bird = BirdData::from([
            'name' => 'Alice',
            'weight' => 12.34,
        ]);

        $dog = DogData::from([
            'name' => 'Bob',
            'color' => 'black',
        ]);

        $house = HouseData::from([
            'animal' => $bird,
        ]);

or

        $house = HouseData::from([
            'animal' => $dog,
        ]);

Now, I want to save this in the DB, to achieve that I am using json_encode($house)

"{"animal":{"class":"App\\Http\\Controllers\\BirdData","name":"Alice","weight":12.34}}"
"{"animal":{"class":"App\\Http\\Controllers\\DogData","name":"Bob","color":"black"}}"

Great! However... the problem is how to get the data object again from the json_decode($data)

That's why I am doing all this, I don't know if there is any better way to achieve this behaviour

from laravel-data.

rubenvanassche avatar rubenvanassche commented on August 29, 2024

We do not support resolve with parameters at the moment. Use the solution koomai presents or write a custom creation method for string. Maybe the eloquent casts can be usefull?

from laravel-data.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.