VarboxInternational / varbox

THE Laravel Admin Panel package built by developers, for developers
https://varbox.io
Other
63 stars 13 forks source link

Invalid argument supplied for foreach() - displayMetaTags() error on freshly created model instances #19

Closed tonyneurony closed 3 years ago

tonyneurony commented 3 years ago

Description of the bug (screenshots might help):

When you add a HasMetaTags trait to a model, that previously had some entries in DB (for example i seed pages for Page model), meta column remains null in DB, until you save that model in DB(so it saves the json data to meta column), you will be getting 'Invalid argument supplied for foreach()' error. The fix is quick: in displayMetaTags() method, wrap the foreach into if (!empty($tags)) {

Steps to reproduce (fresh Varbox installation):

zbiller commented 3 years ago

Hi @tonyneurony

Can you please let me know if you casted the meta column to an array as per the docs?

/**
 * The attributes that should be casted to native types.
 *
 * @var array
 */
protected $casts = [
    ...
    'meta' => 'array',
];
tonyneurony commented 3 years ago

Hi @zbiller Yes, for sure. This actually happens on default Page model as well, when, for example u have a page seeded via seeder, thus the meta columns remains 'null' in DB

zbiller commented 3 years ago

@tonyneurony

I've applied a fix for this issue. It's available on version 2.2.0.

Thanks for your support!