Installation:
composer require lastdragon-ru/lara-asp-eloquent
Ensure your composer.json meets the package's PHP/Laravel version constraints (e.g., Laravel 10+ for modern features).
Publish Config (if needed):
php artisan vendor:publish --provider="LastDragon\LaraAspEloquent\LaraAspEloquentServiceProvider" --tag="config"
(Check if the package includes default config; likely minimal or none.)
First Use Case:
Use the hasManyThrough or morphToMany traits directly in your Eloquent models. Example:
use LastDragon\LaraAspEloquent\Traits\HasManyThrough;
class User extends Model
{
use HasManyThrough;
public function roles()
{
return $this->hasManyThrough(Role::class, UserRole::class, 'user_id', 'id');
}
}
src/Traits/ for available extensions (e.g., HasManyThrough, MorphToMany, SoftDeletes).tests/ for real-world usage patterns (if available).Enhancing Relationships:
HasManyThrough: Simplify multi-table relationships without manual joins.
// User → UserRole → Role
$user->roles; // Direct access to roles via UserRole pivot.
MorphToMany: Polymorphic many-to-many relationships.
class Taggable extends Model
{
use MorphToMany;
public function tags()
{
return $this->morphToMany(Tag::class, 'taggable');
}
}
Soft Deletes:
SoftDeletes trait for global soft-delete behavior:
use LastDragon\LaraAspEloquent\Traits\SoftDeletes;
class Post extends Model
{
use SoftDeletes;
protected $dates = ['deleted_at'];
}
Query Scopes:
scopeActive():
Post::active()->get(); // Filters soft-deleted records.
Model Events:
deleting) to trigger custom logic:
protected static function bootSoftDeletes()
{
static::deleting(function ($model) {
// Custom logic before soft delete.
});
}
save(), delete()).#[HasManyThrough(Role::class, UserRole::class)]
public function roles() {}
$user = new User();
$user->setRelation('roles', collect([new Role()]));
Version Mismatches:
match expressions).composer.json to avoid silent failures.Trait Conflicts:
HasManyThrough or MorphToMany, ensure you’re not duplicating logic.Soft Deletes Ambiguity:
SoftDeletes trait might conflict with Laravel’s built-in SoftDeletes if not namespaced properly.use LastDragon\LaraAspEloquent\Traits\SoftDeletes as LaraAspSoftDeletes;
Missing Documentation:
DB::enableQueryLog();
$user->roles; // Inspect DB::getQueryLog().
class DebugHasManyThrough {
public function __call($method, $args) { dd($method, $args); }
}
Custom Traits:
HasOneThrough):
namespace App\Traits;
use LastDragon\LaraAspEloquent\Traits\HasManyThrough as BaseTrait;
trait HasOneThrough extends BaseTrait { ... }
Macros:
QueryBuilder::macro('withTrashed', function () {
return $this->with(['trashed' => function ($query) {
$query->whereNull('deleted_at');
}]);
});
Event Listeners:
deleting):
event(new Deleting($model));
HasManyThrough, cache the intermediate model’s IDs to reduce queries:
protected function getThroughModelIds()
{
return $this->relationLoaded('userRole') ? $this->userRole->pluck('role_id') : [];
}
Model::newQueryWithoutScopes():
$query = $this->newQueryWithoutScopes()->withTrashed();
public function toArray($request)
{
return [
'roles' => $this->roles->pluck('name'),
];
}
How can I help you explore Laravel packages today?