PHP code example of cxuan1225 / laravel-api-from-table

1. Go to this page and download the library: Download cxuan1225/laravel-api-from-table 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/ */

    

cxuan1225 / laravel-api-from-table example snippets


public function store(StoreCustomerRequest $request): CustomerResource
{
    $customer = $this->storeCustomerAction->handle(
        StoreCustomerData::fromRequest($request),
    );

    return new CustomerResource($customer);
}

Route::apiResource('legacy-users', \App\Http\Controllers\LegacyUserController::class);

$this->getJson('/api/legacy-users')->assertOk();



namespace App\Models;

use Illuminate\Database\Eloquent\Model;

final class Customer extends Model
{
    protected $fillable = [
        'company_id',
        'name',
        'email',
        'credit_limit',
        'is_active',
    ];

    protected function casts(): array
    {
        return [
            'company_id' => 'integer',
            'credit_limit' => 'decimal:2',
            'is_active' => 'boolean',
        ];
    }
}

public function rules(): array
{
    return [
        'company_id' => [' ['nullable', 'string', 'email', 'max:255'],
        'credit_limit' => ['nullable', 'numeric'],
        'is_active' => ['nullable', 'boolean'],
    ];
}

'routes' => [
    'resource_name_style' => 'kebab',
    'prefixes' => [
        'routes' => '',
        'api_routes' => 'api',
    ],
],
bash
php artisan make:model Customer -crR
txt
app/Models/Customer.php
app/Http/Requests/StoreCustomerRequest.php
app/Http/Requests/UpdateCustomerRequest.php
app/Data/StoreCustomerData.php
app/Data/UpdateCustomerData.php
app/Actions/Customers/StoreCustomerAction.php
app/Actions/Customers/UpdateCustomerAction.php
app/Http/Resources/CustomerResource.php
app/Http/Controllers/CustomerController.php
txt
config/api-from-table.php