PHP code example of steven-fox / eloquaint

1. Go to this page and download the library: Download steven-fox/eloquaint library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

steven-fox / eloquaint example snippets


#[Scope('published', static function ($query) {$query->whereNotNull('published_at')->where('published_at', '<=', now())})]

#[HasMany(Notification::class, 'readNotifications', static function ($query) {$query->whereNotNull('read_at')})]

class Author extends Model
{
    public function posts(): HasMany
    {
        return $this->hasMany(Post::class);
    }

    public function publishedPosts(): HasMany
    {
        return $this->hasMany(Post::class)->where('published', true);
    }

    public function scopeActive($query)
    {
        return $query->where('active', true);
    }
}

use StevenFox\Eloquaint\Attributes\HasMany;
use StevenFox\Eloquaint\Attributes\Scope;
use StevenFox\Eloquaint\Traits\HasEloquaintFeatures;

#[HasMany(Post::class)]
#[HasMany(Post::class, name: 'publishedPosts', where: ['published' => true])]
#[Scope('active', 'active', true)]
class Author extends Model
{
    use HasEloquaintFeatures;

    // That's it! No boilerplate methods needed.
}

use StevenFox\Eloquaint\Attributes\HasMany;

#[HasMany(Post::class)]
#[HasMany(Comment::class)]
class Author extends Model
{
    use HasEloquaintFeatures;
}

use StevenFox\Eloquaint\Attributes\HasOne;

#[HasOne(Profile::class)]
class User extends Model
{
    use HasEloquaintFeatures;
}

use StevenFox\Eloquaint\Attributes\BelongsTo;

#[BelongsTo(Author::class)]
#[BelongsTo(Category::class)]
class Post extends Model
{
    use HasEloquaintFeatures;
}

use StevenFox\Eloquaint\Attributes\BelongsToMany;

#[BelongsToMany(Tag::class)]
#[BelongsToMany(Category::class, table: 'post_categories')]
class Post extends Model
{
    use HasEloquaintFeatures;
}

use StevenFox\Eloquaint\Attributes\HasManyThrough;
use StevenFox\Eloquaint\Attributes\MorphMany;

#[HasManyThrough(Comment::class, through: Post::class)]
#[MorphMany(Image::class, name: 'imageable')]
class Author extends Model
{
    use HasEloquaintFeatures;
}

#[HasMany(Post::class, name: 'articles')]
#[HasMany(Post::class, name: 'publishedArticles', where: ['status' => 'published'])]
class Author extends Model
{
    use HasEloquaintFeatures;
}

// Usage:
$author->articles; // All posts
$author->publishedArticles; // Only published posts

#[HasMany(Post::class, where: ['published' => true, 'featured' => true])]
class Author extends Model
{
    use HasEloquaintFeatures;
}

#[BelongsTo(User::class, foreignKey: 'user_id', ownerKey: 'id')]
#[HasMany(Comment::class, foreignKey: 'post_id', localKey: 'id')]
class Post extends Model
{
    use HasEloquaintFeatures;
}

class Author extends Model
{
    use HasEloquaintFeatures;

    #[HasMany(Post::class)]
    protected $posts;

    #[HasMany(Post::class, where: ['published' => true])]
    protected $publishedPosts;
}

use StevenFox\Eloquaint\Attributes\Scope;

#[Scope('published', 'published', true)]           // WHERE published = true
#[Scope('draft', 'published', false)]              // WHERE published = false
#[Scope('popular', 'views', '>', 1000)]            // WHERE views > 1000
class Post extends Model
{
    use HasEloquaintFeatures;
}

// Usage
$publishedPosts = Post::published()->get();
$popularPosts = Post::popular()->get();

#[Scope('published', 'published', true)]
#[Scope('popular', 'views', '>', 1000)]
class Post extends Model
{
    use HasEloquaintFeatures;

    // Use traditional scope methods for complex logic
    public function scopeRecent($query, $days = 7)
    {
        return $query->where('created_at', '>=', now()->subDays($days));
    }

    public function scopeTrending($query)
    {
        return $query->where('views', '>=', 1000)->where('likes', '>=', 10);
    }
}

// Usage
$publishedPosts = Post::published()->get();     // Attribute scope
$popularPosts = Post::popular()->get();         // Attribute scope
$recentPosts = Post::recent(14)->get();         // Traditional scope
$trendingPosts = Post::trending()->get();       // Traditional scope

$posts = Post::published()
    ->where('title', 'like', '%Laravel%')
    ->with('author')
    ->orderBy('created_at', 'desc')
    ->get();

// For multiple scopes, apply them to the base query
$recentPublishedPosts = Post::published()->where('created_at', '>=', now()->subDays(7))->get();
$popularPosts = Post::popular()->get();