Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check relationship exist for Eloequent polymorphic?

Hi how can I check for Eloquent polymorphic relationship already exist?

For example, I want to prevent data with same upload_id, attachable_id and attachable_type from getting store in the attachments table (refer to the highlight)

What I have try but did not get correct count of relationship exist:

//get count of attachment
$application->has('attachments')->count()

enter image description here

like image 993
cyberfly Avatar asked Oct 20 '25 14:10

cyberfly


1 Answers

You can just query the related attachments for that upload_id:

$hasAttachments = $application->attachments()
    ->where('upload_id', $upload_id)
    ->count() > 0;
like image 120
Jerodev Avatar answered Oct 23 '25 09:10

Jerodev