Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

msyaukat's avatar

hasOne query existence.

how do I query hasOne existence? What I want to achieve is, if the user has not register their company's contact, Add Company Contact will appear.

this is my attempt:

controller

$companyContact = Company::find($id)->companycontact()->get();

view

@if($companyContact->isEmpty())
     <p><a href="#">Add Company Contact</a><p>
@endif
0 likes
1 reply
richard's avatar
// App\Company.php

return contact()
{
    return $this->hasOne(Contact::class);
}

// App\Http\Controllers\CompanyController.php

$company = Company::findOrFail($id);


// Your View

@if($company->contact)
     <p><a href="#">Add Company Contact</a><p>
@endif

1 like

Please or to participate in this conversation.