You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// One-to-Many (with foreign id)
public function hero()
{
return $this->hasOne(Attachment::class, 'id', 'hero')->withDefault();
}
// Many-to-Many (no foreign id on table, should be uploaded with groups() function)
public function documents()
{
return $this->hasMany(Attachment::class)->where('group','documents');
}
public function hero()
{
return $this->hasOne(Attachment::class, 'id', 'hero')->withDefault();
}
However, this doesn't work. If I try this:
public function files()
{
return $this->hasMany(Attachment::class)->where('group', 'files');
}
...I get this error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'attachments.device_id' in 'where clause' (SQL: select * from `attachments` where `attachments`.`device_id` = 3 and `attachments`.`device_id` is not null and `group` = files)
Besides, this doesn't seem to make sense. My device doesn't have Attachments – it has Attachmentables. So it's not surprising that I get this error.
2. How can I allow users to delete attached files?
public function createOrUpdate(Post $post, Request $request)
{
$post->fill($request->get('post'))->save();
$post->attachment()->syncWithoutDetaching(
$request->input('post.attachment', [])
);
Alert::info('You have successfully created an post.');
return redirect()->route('platform.post.list');
}
But if a user removes an attachment in the form, and saves the Device, the attachment won't actually be removed from the model. This is the expected behavior of syncWithoutDetaching(). Is this an error in the Orchid docs?
If I use sync() instead of syncWithoutDetaching(), then this almost works: if I remove an attachment in the form, then the Attachmentable gets deleted.
However, the Attachment does not get deleted – even if it has no remaining Attachmentables.
How can I make sure that the Attachment (and the associated file) get deleted, as well? Do I need to handle this manually? And shouldn't this be mentioned in the docs?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Hi there –
Orchid is great! However, the docs are sometimes confusing. And the docs for Attachments are really confusing.
I have a screen for editing Device models. I'm trying to configure an Upload field, which allows users to attach zero or more files to a company:
My specific questions:
1. What is the correct way to define the
files()
method on the Device model?If I don't define this method, then the Upload field will be empty, even when editing a Device that already has attachments.
This seems to work:
But I don't remember where I found this solution, and it may be incorrect. (I don't think it appears in the Orchid docs.)
The only examples I can find in the Orchid docs define it as a standard Eloquent relationship (
hasOne()
orhasMany()
).For example, the docs for the Upload field provide this example:
And the docs for Attachments provide this example:
However, this doesn't work. If I try this:
...I get this error:
Besides, this doesn't seem to make sense. My device doesn't have Attachments – it has Attachmentables. So it's not surprising that I get this error.
2. How can I allow users to delete attached files?
The Manage Attachments tutorial provide this example:
But if a user removes an attachment in the form, and saves the Device, the attachment won't actually be removed from the model. This is the expected behavior of syncWithoutDetaching(). Is this an error in the Orchid docs?
If I use
sync()
instead ofsyncWithoutDetaching()
, then this almost works: if I remove an attachment in the form, then the Attachmentable gets deleted.However, the Attachment does not get deleted – even if it has no remaining Attachmentables.
How can I make sure that the Attachment (and the associated file) get deleted, as well? Do I need to handle this manually? And shouldn't this be mentioned in the docs?
Thanks for any insight you can offer!
Beta Was this translation helpful? Give feedback.
All reactions