PHP code example of yassin-ahmed / laravel-api-crud-generator

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

    

yassin-ahmed / laravel-api-crud-generator example snippets


   // In routes/api.php
   

// In StoreProductRequest.php
public function rules()
{
    return [
        'name' => 'max:1000',
    ];
}

// In ProductResource.php
public function toArray($request)
{
    return [
        'id' => $this->id,
        'name' => $this->name,
        'price' => number_format($this->price, 2),
        'description' => $this->description,
        'formatted_price' => '$' . number_format($this->price, 2),
        'category' => new CategoryResource($this->whenLoaded('category')),
        'created_at' => $this->created_at->format('Y-m-d H:i:s'),
        'updated_at' => $this->updated_at->format('Y-m-d H:i:s'),
    ];
}
bash
php artisan vendor:publish --provider="Yassin\LaravelApiCrudGenerator\ServiceProvider"

app/
├── Models/
│   └── YourModel.php
├── Http/
│   ├── Controllers/Api/
│   │   └── YourModelController.php
│   ├── Requests/YourModel/
│   │   ├── StoreYourModelRequest.php
│   │   └── UpdateYourModelRequest.php
│   └── Resources/
│       └── YourModelResource.php
database/
├── migrations/
│   └── xxxx_xx_xx_xxxxxx_create_your_models_table.php
├── factories/
│   └── YourModelFactory.php
└── seeders/
    └── YourModelSeeder.php
routes/api/
└── YourModel.php
tests/Feature/YourModel/
└── YourModelApiTest.php
bash
# Run all tests
php artisan test

# Run specific model tests
php artisan test tests/Feature/Product/ProductApiTest.php

# Run with coverage
php artisan test --coverage
bash
   php artisan migrate
   
bash
   php artisan db:seed --class=ProductSeeder