$post = Post::with('auditors')->first();
// Get the creator
$post->auditors->creator;
// Get the updater
$post->auditors->updater;
// Get the deleter
$post->auditors->deleter;
// also works with lazy loading
$post = Post::find(7);
// Get the creator
$post->auditors->creator;
// Get the updater
$post->auditors->updater;
// Get the deleter
$post->auditors->deleter;
use Mrpunyapal\LaravelExtendedRelationships\HasExtendedRelationships;
class User extends Model{
use HasExtendedRelationships;
public function audited(){
return $this->hasManyKeys(
related: Post::class,
relations: [
'created_by' => 'created',
'updated_by' => 'updated',
'deleted_by' => 'deleted',
],
localKey: 'id'
);
}
}
$user = User::with('audited')->first();
// Get posts created by the user
$user->audited->created;
// Get posts updated by the user
$user->audited->updated;
// Get posts deleted by the user
$user->audited->deleted;
// also works with lazy loading
$user = User::find(71);
// Get posts created by the user
$user->audited->created;
// Get posts updated by the user
$user->audited->updated;
// Get posts deleted by the user
$user->audited->deleted;
use Mrpunyapal\LaravelExtendedRelationships\HasExtendedRelationships;
class User extends Model
{
use HasExtendedRelationships;
protected $casts=[
'companies' => 'array'
];
public function myCompanies()
{
return $this->hasManyArrayColumn(
related: Company::class,
foreignKey: 'id',
localKey: 'companies'
);
}
}
$user = User::with('myCompanies')->first();
// get companies with ids 7 and 71
$user->myCompanies;
use Mrpunyapal\LaravelExtendedRelationships\HasExtendedRelationships;
class Company extends Model
{
use HasExtendedRelationships;
public function companyFounders()
{
return $this->belongsToArrayColumn(
related: User::class,
foreignKey: 'id',
localKey: 'companies',
// optional, default is false (if true then it treats all values as string)
isString: true
);
}
}
$company = Company::with('companyFounders')->find(71);
// Founders for company with id 71
$company->companyFounders;
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.