Giter Site home page Giter Site logo

Comments (5)

EspadaV8 avatar EspadaV8 commented on August 22, 2024

Could there be any instances where you might want laravels 'dumb' casting of JSON data to an array/object for a column and not this package? Or would should another cast be introduced? e.g. jsond ?

from dialect.

EspadaV8 avatar EspadaV8 commented on August 22, 2024

Ah, looking at the code it wouldn't actually be possible to use this property, the part of Laravel that deals with it...

/**
 * Cast an attribute to a native PHP type.
 *
 * @param  string  $key
 * @param  mixed   $value
 * @return mixed
 */
protected function castAttribute($key, $value)
{
    if (is_null($value)) return $value;

    switch ($this->getCastType($key))
    {
        case 'int':
        case 'integer':
            return (int) $value;
        case 'real':
        case 'float':
        case 'double':
            return (float) $value;
        case 'string':
            return (string) $value;
        case 'bool':
        case 'boolean':
            return (bool) $value;
        case 'object':
            return json_decode($value);
        case 'array':
        case 'json':
            return json_decode($value, true);
        default:
            return $value;
    }
}

Doesn't call any external methods or provide any way to add new casts. The only way would be to override the method...

Actually, that might work out okay, but possibly not the best solution.... hmm

from dialect.

darrylkuhn avatar darrylkuhn commented on August 22, 2024

Conclusion on this? Looking to close out or implement if there's something to do here...

from dialect.

younes0 avatar younes0 commented on August 22, 2024

My first comment was confusing, please note that you can cast to json too (thanks to @EspadaV8)
You could do:

public function inspectJsonColumns()
{
    // merge jsonColumns with cast property
    $casted = array_keys(array_where($this->casts, function($k, $v) {
        return ($v === 'json')
    }));

    $this->jsonColumns = array_merge($this->jsonColumns, $casted);

    foreach ($this->jsonColumns as $col) {
        $this->hidden[] = $col;
        $obj = json_decode($this->$col);
        if (is_object($obj)) {
            foreach ($obj as $key => $value) {
                $this->flagJsonAttribute($key, $col);
                $this->appends[] = $key;
            }
        }
    }
}

from dialect.

darrylkuhn avatar darrylkuhn commented on August 22, 2024

This looks dead but if there is still a need please re-open or submit pull request. Thanks!

from dialect.

Related Issues (8)

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.