PHP code example of mamunhoque / laravel-crud-builder

1. Go to this page and download the library: Download mamunhoque/laravel-crud-builder 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/ */

    

mamunhoque / laravel-crud-builder example snippets


// Example migration: create_posts_table.php
Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->string('title');
    $table->string('slug')->unique();
    $table->text('content')->nullable();
    $table->string('featured_image')->nullable();
    $table->enum('status', ['draft', 'published', 'archived'])->default('draft');
    $table->boolean('is_featured')->default(false);
    $table->decimal('price', 8, 2)->nullable();
    $table->foreignId('category_id')->constrained()->onDelete('cascade');
    $table->foreignId('user_id')->constrained()->onDelete('cascade');
    $table->json('metadata')->nullable();
    $table->timestamps();
    $table->softDeletes();
});

class Post extends Model
{
    use HasFactory, SoftDeletes;

    protected $fillable = [
        'title', 'slug', 'content', 'featured_image', 'status', 
        'is_featured', 'price', 'category_id', 'user_id', 'metadata'
    ];

    protected $casts = [
        'is_featured' => 'boolean',
        'price' => 'decimal:2',
        'metadata' => 'array',
    ];

    protected $appends = ['featured_image_url'];

    public function category(): BelongsTo
    {
        return $this->belongsTo(Category::class);
    }

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    public function getFeaturedImageUrlAttribute(): ?string
    {
        if ($this->featured_image) {
            return config('filesystems.disks.s3.url') . '/' . $this->featured_image;
        }
        return null;
    }
}

class PostService
{
    use HelperTrait;

    public function index($request): Collection|LengthAwarePaginator|array
    {
        $query = Post::query();
        
        $query->with(['category', 'user']);

        // Sorting
        $this->applySorting($query, $request);

        // Searching
        $searchKeys = ['title', 'content'];
        $this->applySearch($query, $request->input('search'), $searchKeys);

        // Apply filters
        if ($request->filled('category_id')) {
            $query->where('category_id', $request->input('category_id'));
        }
        
        if ($request->filled('status')) {
            $query->where('status', $request->input('status'));
        }

        return $this->paginateOrGet($query, $request);
    }
}

class StorePostRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'title' => 'atured_image' => 'nullable|string',
            'status' => 'ullable|exists:users,id',
            'metadata' => 'nullable|array',
        ];
    }
}

// config/crud-builder.php
return [
    'defaults' => [
        'generate_model' => true,
        'generate_controller' => true,
        'generate_service' => true,
        'generate_requests' => true,
        'generate_resource' => true,
        'generate_tests' => true,
        'generate_routes' => true,
        'generate_factory' => true,
    ],

    'routes' => [
        'middleware_groups' => [
            'admin' => ['auth:api', 'role:admin'],
            'public' => [],
            'auth' => ['auth:api'],
        ],
        'default_middleware' => 'admin',
    ],

    'model' => [
        'use_soft_deletes' => true,
        'use_timestamps' => true,
        'generate_relationships' => true,
        'generate_accessors' => true,
    ],

    'validation' => [
        'generate_smart_rules' => true,
        '

// Automatically generated in service
if ($request->filled('category_id')) {
    $query->where('category_id', $request->input('category_id'));
}

// In service
$data['featured_image'] = $this->s3FileUpload($request, 'featured_image', 'posts')['path'] ?? null;

// In model
public function getFeaturedImageUrlAttribute(): ?string
{
    if ($this->featured_image) {
        return config('filesystems.disks.s3.url') . '/' . $this->featured_image;
    }
    return null;
}

// Pagination and filtering
$this->applySorting($query, $request);
$this->applySearch($query, $searchValue, $searchKeys);
$this->paginateOrGet($query, $request);

// File uploads
$this->s3FileUpload($request, 'image', 'uploads');
$this->localFileUpload($request, 'file', 'documents');

// API responses
$this->successResponse($data, 'Success message');
$this->errorResponse($errors, 'Error message', 422);

// E-commerce specific filters
$this->applyEComFilters($query, $request);
$this->applyEComSorting($query, $request);

// Feature test
public function it_can_create_post()
{
    $data = [
        'title' => fake()->sentence(),
        'content' => fake()->paragraph(),
        'status' => fake()->randomElement(['draft', 'published', 'archived']),
        'category_id' => Category::factory()->create()->id,
    ];

    $response = $this->postJson(route('admin.posts.store'), $data);

    $response->assertStatus(201);
    $this->assertDatabaseHas('posts', $data);
}
bash
php artisan vendor:publish --tag="crud-builder-config"
bash
php artisan vendor:publish --tag="crud-builder-stubs"
bash
php artisan crud-builder:publish-helper-trait
bash
php artisan make:advanced-crud Post
bash
# Generate for public API (no authentication)
php artisan make:advanced-crud Post --middleware=public

# Generate for authenticated users
php artisan make:advanced-crud Post --middleware=auth

# Custom route prefix
php artisan make:advanced-crud Post --prefix=v1/api