Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loss of functionality by casting to array, should be changed. #1033

Open
OzanKurt opened this issue Feb 28, 2017 · 7 comments
Open

Loss of functionality by casting to array, should be changed. #1033

OzanKurt opened this issue Feb 28, 2017 · 7 comments

Comments

@OzanKurt
Copy link
Contributor

In src/Helper.php, why would I cast a useful object to an array and lose all the functionality they provide?

For example:

I have a scheduled_at column, which should be rendered as d-m-Y H:i, and I can't use ->format() on it because it has been cast to an array from a Carbon\Carbon object.

    /**
     * Cast the parameter into an array.
     *
     * @param mixed $param
     * @return array
     */
    public static function castToArray($param)
    {
        if ($param instanceof \stdClass) {
            $param = (array) $param;

            return $param;
        }

        if ($param instanceof Arrayable) {
            return $param->toArray();
        }

        return $param;
    }
@OzanKurt
Copy link
Contributor Author

    /**
     * Get a mixed value of custom data and the parameters.
     *
     * @param  array $data
     * @param  mixed $param
     * @return array
     */
    public static function getMixedValue(array $data, $param)
    {
//        $param = self::castToArray($param);

        foreach ($data as $key => $value) {
            if (isset($param[$key])) {
                $data[$key] = $param[$key];
            }
        }

        return $data;
    }

I commented out the part where it casts the data to array, please let me know if this harms my app in any way.

@yajra
Copy link
Owner

yajra commented Mar 1, 2017

Can't recall the idea behind it but I think this is also related to this #641.

@OzanKurt
Copy link
Contributor Author

OzanKurt commented Mar 1, 2017

:/ It seem to work well without that, for me.

@yajra
Copy link
Owner

yajra commented Jul 6, 2017

@OzanKurt your use case for this is on blade templating right?

->addColumn('html', 'path.to.view')

@yajra yajra added this to the v8.0 milestone Jul 6, 2017
@OzanKurt
Copy link
Contributor Author

OzanKurt commented Jul 6, 2017

yes

@yajra
Copy link
Owner

yajra commented Jul 6, 2017

Copy! on v8, I added a model key that will be passed on the view. You can then use it for all necessary functionalities. All attributes will still be casted to array variables though.

// action.blade.php
{{ $model->scheduled_at->format('Y-m-d') }}
{{ $id }}

@yajra
Copy link
Owner

yajra commented Jul 6, 2017

BTW, can I get your input on issue #1246? Do you think it's a good move to change the namespace or it will be a hassle for everyone to upgrade? I can revert to old namespace to lessen the BC if others don't like it. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants