Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue in the file-handling when using loadCount() on model #19

Open
rbruhn opened this issue Sep 16, 2024 · 1 comment · May be fixed by #21
Open

Issue in the file-handling when using loadCount() on model #19

rbruhn opened this issue Sep 16, 2024 · 1 comment · May be fixed by #21

Comments

@rbruhn
Copy link

rbruhn commented Sep 16, 2024

I've ran into a situation where I cannot use loadCount() on a model when the model is using laravel-paperclip.
The error is pasted below and comes from this package.

$project = Project::find(13);
$project->loadCount('expeditions');
dd($project->expeditions_count);

The above results in the following error:

 Could not interpret given data, string value expected

  at vendor/czim/file-handling/src/Storage/File/StorableFileFactory.php:104
    100▕             $data = new RawContent($data);
    101▕         }
    102▕
    103▕         if (! ($data instanceof RawContentInterface)) {
  ➜ 104▕             throw new UnexpectedValueException('Could not interpret given data, string value expected');
    105▕         }
    106▕
    107▕         return $this->interpretFromRawContent($data, $name, $mimeType);
    108▕     }

      +10 vendor frames 

  11  app/Console/Commands/AppCommand.php:56
      Illuminate\Database\Eloquent\Model::loadCount()
      +12 vendor frames 

I have two models, Project and Expedition, that use Laravel-Paperclip. I tried loading counts of other relations that do not use laravel-paperclip and they don't work either. Example:

$project->loadCount('expeditions'); // returns error
$project->loadCount('panoptes'); // returns error
$expedition->loadCount('events'); // returns error

If I use a model that doesn't use Laravel-Paperclip, the loadCount() works as expected.
Edit: Querying withCount() works as expected. I use loadCount() in this situation because it's a route using model binding.

Any idea on how to fix this?

Oh... an example of my Project model... real simple:

public function __construct(array $attributes = [])
{
    $this->hasAttachedFile('logo', [
        'url' => config('config.missing_project_logo'),
    ]);

    parent::__construct($attributes);
}
@rbruhn
Copy link
Author

rbruhn commented Sep 16, 2024

Not sure if this helps but the $data variable causing the error in StorableFileFactory.php is is an instance of "Czim\Paperclip\Attachment\Attachment"

I serialized the $data variable before reaching the is_string() check in StorableFileFactory line 98 to see if it would work and it does. It works with all model->loadCount() as well as model->withCount(). Might not be the solution you want but just an idea.

// Added to handle loadCount error
if (! is_string($data)) {
    $data = serialize($data);
}

// Fallback: expect raw or string data, and attempt to interpret it.
if (is_string($data)) {
   $data = new RawContent($data);
}

All the below iterations work with the fix above.

// Parent and Child have attachments
$project = Project::withCount('expeditions')->find(13);
echo $project->expeditions_count.PHP_EOL;
$project = Project::find(13);
$project->loadCount('expeditions');
echo $project->expeditions_count.PHP_EOL;

// Only Parent has attachement
$expedition = Expedition::withCount('downloads')->find(28);
echo $expedition->downloads_count.PHP_EOL;
$expedition = Expedition::find(28);
$expedition->loadCount('downloads');
echo $expedition->downloads_count.PHP_EOL;

// Parent has no attachment, Child does
$group = Group::withCount('projects')->find(22);
echo $group->projects_count.PHP_EOL;
$group = Group::find(22);
$group->loadCount('projects');
echo $group->projects_count.PHP_EOL;

// Parent and Child have no attachments
$events = Event::withCount('teams')->find(3);
echo $events->teams_count.PHP_EOL;
events = Event::find(3);
$events->loadCount('teams');
echo $events->teams_count.PHP_EOL;

rbruhn added a commit to rbruhn/file-handling that referenced this issue Oct 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant