Framework: Adding CSV in eloquent casts

Created on 2 Apr 2016  路  6Comments  路  Source: laravel/framework

Eloquent casts is a nice feature but currently, there is no option for CSV type.

Is there any chance of adding CSV cast in Eloquent Model. So, an array will be converted to CSV values and saved in DB. castAttribute() and setAttribute() can be changed to

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 $this->fromJson($value, true);
        case 'array':
        case 'json':
            return $this->fromJson($value);
        case 'csv':
            return $this->fromCsv($value);
        case 'collection':
            return new BaseCollection($this->fromJson($value));
        case 'date':
        case 'datetime':
            return $this->asDateTime($value);
        case 'timestamp':
            return $this->asTimeStamp($value);
        default:
            return $value;
    }
}

public function setAttribute($key, $value)
{
    // First we will check for the presence of a mutator for the set operation
    // which simply lets the developers tweak the attribute as it is set on
    // the model, such as "json_encoding" an listing of data for storage.
    if ($this->hasSetMutator($key)) {
        $method = 'set'.Str::studly($key).'Attribute';

        return $this->{$method}($value);
    }

    // If an attribute is listed as a "date", we'll convert it from a DateTime
    // instance into a form proper for storage on the database tables using
    // the connection grammar's date format. We will auto set the values.
    elseif ($value && (in_array($key, $this->getDates()) || $this->isDateCastable($key))) {
        $value = $this->fromDateTime($value);
    }

    if ($this->isJsonCastable($key) && ! is_null($value)) {
        $value = $this->asJson($value);
    }

    if ($this->isCsvCastable($key) && ! is_null($value)) {
        $value = $this->asCsv($value);
    }

    $this->attributes[$key] = $value;

    return $this;
}

And three new functions will be added isCsvCastable(), fromCsv() and asCsv()

protected function isCsvCastable($key)
{
    return $this->hasCast($key, ['csv']);
}

protected function asCsv($value)
{
    return implode(',', $value);
}

public function fromCsv($value)
{
    return explode(',', $value);
}

I can generate a PR if this seems like a good addition.

All 6 comments

Whats the benefit/use case here?

I could see having a toCSV() method on collections (if there isn't one already... not sure on that) but I don't see the need to be converting to/from csv that often directly out of the DB...

I'm not sure the use case is large enough to put this in the code, especially when these functions you have here don't actually work with input that already contains commas.

I'd recommend something like https://github.com/thephpleague/csv.

@GrahamCampbell Even I am not sure the use case. That's why I wanted your opinion if it should be included or not.
And the phpleague library is excellent.

Ping @taylorotwell. Do you want this in the core?

I am using this package in production, it does exactly what you're requesting

@DeeJaVu Not sure what those :-1: were for?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gabriellimo picture gabriellimo  路  3Comments

felixsanz picture felixsanz  路  3Comments

PhiloNL picture PhiloNL  路  3Comments

RomainSauvaire picture RomainSauvaire  路  3Comments

progmars picture progmars  路  3Comments