| Server IP : 65.108.144.40 / Your IP : 216.73.217.165 Web Server : Apache/2.4.52 (Ubuntu) System : Linux ubuntu-8gb-hel1-1 5.15.0-173-generic #183-Ubuntu SMP Fri Mar 6 13:29:34 UTC 2026 x86_64 User : dev ( 1000) PHP Version : 8.2.30 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /var/www/html/project-slim/app/Models/ |
Upload File : |
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Builder;
class Company extends BaseModel
{
/**
* The table associated with the model.
*/
protected $table = 'repo_company';
/**
* The primary key for the model.
*/
protected $primaryKey = 'company_id';
/**
* The attributes that are mass assignable.
*/
protected $fillable = [
'name',
'domain'
];
/**
* The attributes that should be hidden for serialization.
* Inherits from BaseModel to hide all ID fields
*/
protected $hidden = [
'id',
'company_id',
'user_id',
'role_id',
'report_id',
'module_id'
];
/**
* Get the users for the company.
*/
public function users(): HasMany
{
return $this->hasMany(User::class, 'company_id', 'company_id');
}
/**
* Get the social links for the company.
*/
public function socialLinks(): HasMany
{
return $this->hasMany(CompanySocial::class, 'company_id', 'company_id');
}
/**
* Get the product links for the company.
*/
public function productLinks(): HasMany
{
return $this->hasMany(CompanyProductLink::class, 'company_id', 'company_id');
}
/**
* Get active users for the company.
*/
public function activeUsers(): HasMany
{
return $this->users()->where('is_active', true);
}
/**
* Get verified users for the company.
*/
public function verifiedUsers(): HasMany
{
return $this->users()->where('is_verified', true);
}
/**
* Get company's LinkedIn link.
*/
public function getLinkedInLinkAttribute(): ?string
{
$linkedin = $this->socialLinks()->where('platform', 'linkedin')->first();
return $linkedin ? $linkedin->url : null;
}
/**
* Get company's Clutch link.
*/
public function getClutchLinkAttribute(): ?string
{
$clutch = $this->productLinks()->where('platform', 'clutch')->first();
return $clutch ? $clutch->url : null;
}
/**
* Get company's G2 link.
*/
public function getG2LinkAttribute(): ?string
{
$g2 = $this->productLinks()->where('platform', 'g2')->first();
return $g2 ? $g2->url : null;
}
/**
* Get user count for the company.
*/
public function getUserCountAttribute(): int
{
return $this->users()->count();
}
/**
* Get active user count for the company.
*/
public function getActiveUserCountAttribute(): int
{
return $this->activeUsers()->count();
}
/**
* Scope to search companies.
*/
public function scopeSearch(Builder $query, string $term): Builder
{
return $query->where(function (Builder $q) use ($term) {
$q->where('name', 'ILIKE', "%{$term}%")
->orWhere('domain', 'ILIKE', "%{$term}%");
});
}
/**
* Scope to filter by domain.
*/
public function scopeByDomain(Builder $query, string $domain): Builder
{
return $query->where('domain', $domain);
}
/**
* Add a social link to the company.
*/
public function addSocialLink(string $platform, ?string $handle = null, ?string $url = null): CompanySocial
{
return $this->socialLinks()->create([
'platform' => $platform,
'handle' => $handle,
'url' => $url
]);
}
/**
* Add a product link to the company.
*/
public function addProductLink(string $platform, string $url): CompanyProductLink
{
return $this->productLinks()->create([
'platform' => $platform,
'url' => $url
]);
}
/**
* Update or create social link.
*/
public function updateSocialLink(string $platform, ?string $handle = null, ?string $url = null): CompanySocial
{
return $this->socialLinks()->updateOrCreate(
['platform' => $platform],
['handle' => $handle, 'url' => $url]
);
}
/**
* Update or create product link.
*/
public function updateProductLink(string $platform, string $url): CompanyProductLink
{
return $this->productLinks()->updateOrCreate(
['platform' => $platform],
['url' => $url]
);
}
/**
* Get company statistics.
*/
public function getStatistics(): array
{
return [
'total_users' => $this->getUserCountAttribute(),
'active_users' => $this->getActiveUserCountAttribute(),
'verified_users' => $this->verifiedUsers()->count(),
'social_links' => $this->socialLinks()->count(),
'product_links' => $this->productLinks()->count(),
];
}
}