Hypens in DB data returning substrings

Published 3 months ago by jdc1898

I have a dataset that stores SKU's in a table (10107686-1282-CS-500) but when I return this column from the database it only returns (10107686) and I can't figure out why. What is odd is that if I dump the returned object, the SKU appear correct.

Example:

    [attributes:protected] => Array
        (
            [sku] => 10107686-1282

the column name is (sku) in the table and I am accessing it by $data->sku

What am I doing wrong here?

Best Answer (As Selected By jdc1898)
shez1983

wait if primary key is sku, do u have it set as autoincremneting = false (or something similar) otherwise laravel by default might treat it as int..

adamprickett

Have you got any casts setup on that model? It looks like it might be being casted as an int

jdc1898

Good thought but no.

    protected $primaryKey = 'sku';
    public $timestamps = false;
    protected $fillable = [...]
Cronix
Cronix
3 months ago (730,880 XP)

Try adding it to the casts array, and cast it to a string.

protected $casts = [
    'sku' => 'string',
];

https://laravel.com/docs/5.6/eloquent-mutators#attribute-casting

shez1983

wait if primary key is sku, do u have it set as autoincremneting = false (or something similar) otherwise laravel by default might treat it as int..

Snapey
Snapey
3 months ago (995,145 XP)

yes, if primary key is not an int you need public $incrementing=false; on the model

jdc1898

@shez1983 You nailed it! Is that something that you just knew or is it documented? Anyway Thank you!

shez1983

it is documented.. laravel docs are cool :)

Please sign in or create an account to participate in this conversation.