Crud: [4.1][Bug] browse_multiple field doesn't work correct if stored as fake.

Created on 27 May 2020  路  3Comments  路  Source: Laravel-Backpack/CRUD

Bug report

What I did

In model

protected $fakeColumns = ['content'];
protected $casts = ['content' => 'array'];

In my Crud controller i have field

CRUD::field('slides')->type('browse_multiple')->label('Images')->fake(true)>store_in('content')->sortable(true)->hint('webp');

What I expected to happen

Field works exactly as non-fake. Showing images after save.

What happened

When i select images and save, value stored in db, like

{"slides": "[\"uploads/webp-test.webp\"]"}

But when i open my Entity in admin, i have empy field. Then, i save it. And in db i have this

{"slides": "\"[\\\"uploads\\/webp-test.webp\\\"]\""}

What I've already tried to fix it

nothing

Backpack, Laravel, PHP, DB version

When I run php artisan backpack:version the output is:

### PHP VERSION:
PHP 7.4.6 (cli) (built: May 14 2020 10:38:36) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.6, Copyright (c), by Zend Technologies

### LARAVEL VERSION:
v7.13.0@6fa69bfbd57744a5bbec5538ce483919b3fd625f

### BACKPACK VERSION:
4.1.6@cbd4143d3eb8302916012af205565cd3183f274f
Bug

Most helpful comment

@pxpm Before I realized that I need to use withFakes() to access fake field as normal field, i'm doing like this json_decode($model->content['slides'], true) to work inside front controller or views. And in common fileds, it's work, without casting fake field.

Right way of using fake field as i understand:

Non translatable fake field stored in fake_content as example

In Crud:

CRUD::field('slides')->type('browse_multiple')->label('Images')->fake(true)>store_in('fake_content');

In Model:

protected $fakeColumns = ['fake_content'];
protected $casts = [
    'fake_content' => 'array',
    'slides' => 'array',
];

In Contoller or view
$model = Model::first()->withFakes();
and now we can working with our fake slides field as normally

foreach ($model->slides as $image) {
    //do something with $image
}

Translatable fake field stored in fake_content_i18n as example

In Crud:

CRUD::field('slides')->type('browse_multiple')->label('Images')->fake(true)>store_in('fake_content_i18n');

In Model:

protected $fakeColumns = ['fake_content_i18n'];
protected $translatable = ['fake_content_i18n'];
protected $casts = [
    'slides' => 'array',
];

In Contoller or view
$model = Model::first()->withFakes();
and now we can working with our fake slides field as normally

foreach ($model->slides as $image) {
    //do something with $image
}

All 3 comments

@pxpm when you answered the previous issue, it became clear to me that I used to work incorrectly with the fake field all the time)
using withFakes()
and cast fake field slides to array solving problem.
Maybe add more documentation to fake field section?)

Can you please explain where you added withFakes() to solve this problem ?

I am going to close the issue but feel free to write it down in comments if you can, it might help other people that faces the same problem.

Best,
Pedro

@pxpm Before I realized that I need to use withFakes() to access fake field as normal field, i'm doing like this json_decode($model->content['slides'], true) to work inside front controller or views. And in common fileds, it's work, without casting fake field.

Right way of using fake field as i understand:

Non translatable fake field stored in fake_content as example

In Crud:

CRUD::field('slides')->type('browse_multiple')->label('Images')->fake(true)>store_in('fake_content');

In Model:

protected $fakeColumns = ['fake_content'];
protected $casts = [
    'fake_content' => 'array',
    'slides' => 'array',
];

In Contoller or view
$model = Model::first()->withFakes();
and now we can working with our fake slides field as normally

foreach ($model->slides as $image) {
    //do something with $image
}

Translatable fake field stored in fake_content_i18n as example

In Crud:

CRUD::field('slides')->type('browse_multiple')->label('Images')->fake(true)>store_in('fake_content_i18n');

In Model:

protected $fakeColumns = ['fake_content_i18n'];
protected $translatable = ['fake_content_i18n'];
protected $casts = [
    'slides' => 'array',
];

In Contoller or view
$model = Model::first()->withFakes();
and now we can working with our fake slides field as normally

foreach ($model->slides as $image) {
    //do something with $image
}
Was this page helpful?
0 / 5 - 0 ratings