lundi 19 août 2019

Laravel Polymorphic get all models associated to a single model

I have an Attachment model which uses the morphs table and morphTo function. I have several models but the main ones include Client, Job, Project and Notes (which is also polymorphic) which can contain several Attachments.

A client is the top-level model. A Client has many Jobs. A Job has many Projects.

I am struggling with a single way to return all attachments of a client, including attachments of each Job, Project, and notes of each job/project under a client.

I currently am running several foreach loops and have a working way, but the queries on the page load range from 60-100 depending on the amount of jobs/projects/notes for each client. I run through each job to check if it has an attachment, if so, I loop through them. Then, I run through $job->notes->attachments and display those. From there, I dive into another foreach loop pulling all the job's projects, pulling the attachments from each project and then pulling all the notes and looping through that.

Is there a way within Laravel to get all of the Attachments that are somehow attached to a single Client without looping through the way I have? If not, is there a way I can optimize my loops so I don't have to request the attachments for each job/job's notes/project/project's notes?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2KVqa4A
via IFTTT

Aucun commentaire:

Enregistrer un commentaire