File Attachment Sharing

I’m hoping somebody can offer up their expertise on how I might share file attachments.

My strata property has 34 units ( 1 - 34 ). Today we had a plumber come in and do a repair for unit #29 where I scan the bill and attach the file to the unit. I have zero problem using “attachmany” in my “Units” model as explained in the documents:

    public $attachMany = [
        'documents' => [\System\Models\File::class, 'public' => false]
    ];

How might I go about sharing specific files between units? A good example of this would be to use Unit #0 which would represent the entire complex. We just had heat pumps installed on all units, so ideally I would like to attach the bill to Unit #0 and then ‘share’ that bill with all 34 units so when I select unit #29, I would get both the Plumbing bill from today as well as the bill for the heat pumps. When an owner clicks on their units, I want to show them a full history of work done on their property so had unit #28 been affected by the plumbing leak today, then #29 and #28 would share a bill together.

I have tried using:

    $unit = Unit::find(29);
    $unit2 = Unit::find(28);
    $file = File::find(1);//The file already exists in the system for simplicity

    $unit->documents()->add($file);
    $unit2->documents()->add($file);

The result in the system_files table is attachment_id = 28 (29 nowhere to be found because attachment_id was overwritten from 29 to 28)

I think the best solution is to have the first unit a file was attached to as the ‘owner’ of the file, and the owner ‘shares’ the file with other units via pivot table? Assuming this is the best way to go about it, how would I implement this? Is this something a relationClass or scope would be used for?

No problem doing the research and figuring it out, I would just love some guidance on what I should be researching. I’m finding asking the right question to get me on track is about 60% of the solution.

Thanks everyone!

Why don’t you create a model called “Bill” that can have relation to multiple units?

Each “bill” can then have attachOne file for it as an example.

Then you can easily share bills between units and even do more with it.

2 Likes

An even better question is why did I not think of such an obvious solution? lol. Tunnel vision has claimed another victim.

Thank you Georgij!

1 Like