laravel-stapler: Object of class Codesleeve\Stapler\Attachment could not be converted to string
Hi,
I use Laravel 4.2, Laravel-stapler 1.0.03. I got this error when saving model that use laravel-stapler:
exception 'ErrorException' with message 'Object of class Codesleeve\Stapler\Attachment could not be converted to string' in /src/vendor/laravel/framework/src/Illuminate/Support/helpers.php:900
Here is my model:
use Codesleeve\Stapler\ORM\StaplerableInterface;
use Codesleeve\Stapler\ORM\EloquentTrait;
class Article extends Elegant implements StaplerableInterface{
use EloquentTrait;
/**
* Fillable
*/
protected $fillable = ['image', 'title', 'summary', 'content'];
public function __construct(array $attributes = array()) {
$this->hasAttachedFile('image', [
'styles' => [
'medium' => '300x300',
'thumb' => '100x100'
]
]);
parent::__construct($attributes);
}
}
The request info:
- Method: PUT
- Input (without image): [“title” => “Article’s title”]
About this issue
- Original URL
- State: open
- Created 9 years ago
- Comments: 17 (4 by maintainers)
This is happening because Eloquent\Model (as of 5.5) is now using
$this->getAttributes()instead of$this->attributeswhen generating the attributes array that will be used to update a model (this issue only seems to be happening when performing updates; When performing inserts$this->attributesis still used and that’s a bit inconsistent/weird too).For reference purposes, the EloquentTrait that comes with Stapler has a method on it that overrides the
getAttributes()method to included any attached files that are defined on the model so that they appear as properties: https://github.com/CodeSleeve/stapler/pull/94I feel like this is more an issue with consistency regarding Eloquent, not Stapler and atm I’m not quite sure the best way to go about resolving this. I may try and talk to Taylor about this and see if he’d be open to a PR to resolve this. If not, I may have to drop a major release that only supports 5.5 going forward, but I would rather not do that right now.
Regardless, for the time being you can just override the
getAttributes()method on your model to not include the attachments defined on it (this effectively bypasses the method defined on the EloquentTrait):Or you can just write your own trait that doesn’t include that method and then just use your trait in place of the
EloquentTrait.Ok, I thought that was weird that you would all be suing Ardent. I’m going to patch this tomorrow evening when I get off work, but I may not be able to get it out until this weekend. PR’s are always welcome too. Regardless, I’ll have a fix out as soon as I can.
Another fix is to modify the results of
getDirtyand removeCodesleeve\Stapler\Attachmentinstances on models which use attachments.getDirtyis only run on updates and is where the issue is originating. So, something like:Used this in a project and was able to keep the decorated
getAttributes()result. I put it into a trait along with other Stapler specific code and was able to update without much trouble.