Laravel-Backpack / CRUD

Build custom admin panels. Fast!
https://backpackforlaravel.com
MIT License
3.18k stars 897 forks source link

[Bug] uploader: overwrite field when update model #5711

Open tjslash opened 3 weeks ago

tjslash commented 3 weeks ago

Bug report

What I did

CRUD article controller with field:

CRUD::addField([
    'name' => 'image',
    'label' => 'Image',
    'type' => 'image',
    'withFiles' => [
        'disk' => 'public',
        'path' => 'storage/images'
    ],
    'tab' => 'Images'
]);

and a FrontController file:

$article = Article::ofSlug($request->route('article'))->firstOrFail();
$article->update([
    'views' => $article->views + 1
]);
  1. Open admin page a CRUD article
  2. Upload image file and save article
  3. Open web page a FrontController

What I expected to happen

The count of views should be increase.

What happened

Views has been increase successful. But each 16th request: image set is NULL;

Query log:

update `articles` set `image` = NULL, `views` = 29, `articles`.`updated_at` = "2024-11-01 00:59:52" where `id` = 1;

Is it a bug in the latest version of Backpack?

Yes.

Backpack, Laravel, PHP, DB version

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

### PHP VERSION:
8.3.4

### PHP EXTENSIONS:
Core, date, libxml, openssl, pcre, sqlite3, zlib, ctype, curl, dom, fileinfo, filter, hash, iconv, json, mbstring, SPL, session, PDO, pdo_sqlite, standard, posix, random, readline, Reflection, Phar, SimpleXML, tokenizer, xml, xmlreader, xmlwriter, mysqlnd, bcmath, exif, gd, intl, mysqli, pcntl, pdo_mysql, redis, soap, sockets, sodium, zip, Zend OPcache, xdebug

### LARAVEL VERSION:
11.28.1.0

### BACKPACK PACKAGE VERSIONS:
backpack/basset: 1.3.6
backpack/crud: 6.7.41
backpack/generators: v4.0.5
backpack/logmanager: v5.0.1
backpack/permissionmanager: 7.2.0
backpack/pro: 2.2.21
backpack/settings: 3.1.1
backpack/theme-coreuiv2: 1.2.3
backpack/theme-coreuiv4: 1.1.1

Octane with RoadRunner:

laravel/octane : 2.3.7
spiral/roadrunner: 2023.3.12
spiral/roadrunner-http: 3.4.0
welcome[bot] commented 3 weeks ago

Hello there! Thanks for opening your first issue on this repo!

Just a heads-up: Here at Backpack we use GitHub Issues only for tracking bugs. Talk about new features is also acceptable. This helps a lot in keeping our focus on improving Backpack. If you issue is not a bug/feature, please help us out by closing the issue yourself and posting in the appropriate medium (see below). If you're not sure where it fits, it's ok, a community member will probably reply to help you with that.

Backpack communication channels:

Please keep in mind Backpack offers no official / paid support. Whatever help you receive here, on Gitter, Slack or Stackoverflow is thanks to our awesome awesome community members, who give up some of their time to help their peers. If you want to join our community, just start pitching in. We take pride in being a welcoming bunch.

Thank you!

-- Justin Case The Backpack Robot

backpack-ai-bot[bot] commented 3 weeks ago

Based on the information provided in your bug report, it appears you're experiencing an issue with the image field being set to NULL unexpectedly when updating an article's view count. This seems to be a potential bug in Backpack CRUD. Let's analyze the situation:

  1. The image field is correctly defined in your CRUD controller:
CRUD::addField([
    'name' => 'image',
    'label' => 'Image',
    'type' => 'image',
    'withFiles' => [
        'disk' => 'public',
        'path' => 'storage/images'
    ],
    'tab' => 'Images'
]);
  1. Your FrontController is updating the view count:
$article = Article::ofSlug($request->route('article'))->firstOrFail();
$article->update([
    'views' => $article->views + 1
]);

The unexpected behavior you're seeing where the image is set to NULL every 16th request is not normal and could indeed be a bug.

There's a known issue in Backpack CRUD related to image fields and updates, as mentioned in this GitHub issue. The issue describes a problem with repeatable fields containing image subfields, but it's possible that your problem is related.

Additionally, there have been other image-related issues reported, such as this one which involved problems with translated image attributes.

Given that you're using the latest versions of Backpack CRUD (6.7.41) and Laravel (11.28.1.0), it's possible that this is a new bug that hasn't been reported yet.

To troubleshoot this issue:

  1. Make sure you're using the latest version of Backpack CRUD and all related packages.
  2. Try adding 'temporary' => true to your image field definition, as suggested in this community forum comment.
  3. If the problem persists, consider creating a new issue on the Backpack CRUD GitHub repository with a detailed description of your setup and the steps to reproduce the bug.

It's also worth noting that you're using Laravel Octane with RoadRunner, which could potentially introduce complexities in how the application state is managed between requests. Make sure to test if the issue persists without Octane to isolate whether it's a Backpack-specific problem or related to the Octane setup.

If you need further assistance or if the problem continues after trying these steps, I recommend reaching out to the Backpack community or creating a new GitHub issue for more targeted support.

Relevant sources:

Github Issue (open): [Bug] CRUD Repeatable field with sub field image. Use withFiles param path broken file after update model. Github Issue (open): [Bug] CRUD Repeatable field with sub field image. Use withFiles param path broken file after update model. Github Issue (closed): [4.0] Deleting the old image while replacing is missing in docs. Github Issue (closed): [Bug] Image field broken image link after upload in BP6 Github Issue (closed): [Bug] error on replacing repeatable subfield image when first saving it empty Github Issue (closed): [Bug] Image/Upload field issue with Translated attribute Github Issue (open): [Bug] CRUD Repeatable field with sub field image. Use withFiles param path broken file after update model. Github Issue (open): [Bug] CRUD Repeatable field with sub field image. Use withFiles param path broken file after update model. Github Issue (closed): [Bug] Cannot find 'image' field view in any of the regular locations. Github Issue (closed): [Bug] Image field broken image link after upload in BP6

manfield commented 3 weeks ago

Hi, as long as you don't inherit your FrontController from any BackPack controller or don't use any BackPack any other traits in your FrontController and Article (except CrudTrait) I doubt it is a Backpack issue.

In general fillable fields are updated by Laravel, retrieving them automatically when you update the model.

public function update(array $attributes = [], array $options = [])
{
    if (! $this->exists) {
        return false;
    }

    return $this->fill($attributes)->save($options);
}

what is really strange is that your image column on the database changes its value to NULL at a certain point. Instead of monitoring the log query, may you refresh the database values for that record at any update and see what happens? (when the image column turns to NULL)

jcastroa87 commented 3 weeks ago

Hello @tjslash

Can you share full Model and FrontController to replicate the issue.

Cheers.