# Create Version API (ok)

![](https://1957079826-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MCBeDUD-PK_RF-YgJRe%2Fuploads%2FoaUWSbz4rXki4Ornpynu%2FScreenshot_1.png?alt=media\&token=3391dd2e-28cd-4eb4-8df9-12d828ccec25)

![](https://1957079826-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MCBeDUD-PK_RF-YgJRe%2Fuploads%2FjvdVnburcpybJ3mn4uEW%2FScreenshot_2.png?alt=media\&token=66f110c8-7d39-4804-b5ec-ebab27217120)

{% file src="https://1957079826-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MCBeDUD-PK_RF-YgJRe%2Fuploads%2Fni69BAYJAcls8LynTHYb%2Fapi.sql?alt=media&token=7f808b47-87ec-4108-90a5-c4cf40da41a8" %}

C:\xampp\htdocs\api\app\Http\Resources\V1\CustomerResource.php

```
<?php
namespace App\Http\Resources\V1;
use Illuminate\Http\Resources\Json\JsonResource;
class CustomerResource extends JsonResource {
  /**
   * Transform the resource into an array.
   *
   * @param  \Illuminate\Http\Request  $request
   * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
   */
  public function toArray($request) {
    // return parent::toArray($request);
    return [
      'id' => $this->id,
      "name_customer" => $this->name_customer
    ];
  }
}
```

C:\xampp\htdocs\api\app\Http\Resources\V2\CustomerResource.php

```
<?php
namespace App\Http\Resources\V2;
use Illuminate\Http\Resources\Json\JsonResource;
class CustomerResource extends JsonResource {
  /**
   * Transform the resource into an array.
   *
   * @param  \Illuminate\Http\Request  $request
   * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
   */
  public function toArray($request) {
    // return parent::toArray($request);
    return [
      'id'               => $this->id,
      "name_customer"    => $this->name_customer,
      'address_customer' => $this->address_customer,
    ];
  }
}
```

C:\xampp\htdocs\api\app\Http\Controllers\Api\V1\CustomerController1.php

```
<?php
namespace App\Http\Controllers\Api\V1;
use App\Models\Customer;
use Illuminate\Http\Request;
use App\Http\Resources\V1\CustomerResource;
use App\Http\Resources\CustomerCollection;
class CustomerController1 extends Controller {
  /**
   * Display a listing of the resource.
   *
   * @return \Illuminate\Http\Response
   */
  public function index() {
    // return Customer::all();
    // return CustomerResource::collection(Customer::paginate());
    // return new CustomerCollection(Customer::paginate());
    return CustomerResource::collection(Customer::paginate());
  }
  /**
   * Show the form for creating a new resource.
   *
   * @return \Illuminate\Http\Response
   */
  public function create() {
    //
  }
  /**
   * Store a newly created resource in storage.
   *
   * @param  \Illuminate\Http\Request  $request
   * @return \Illuminate\Http\Response
   */
  public function store(Request $request) {
    $request->validate([
      'name_customer' => 'required',
      'phone_customer' => 'required',
      'address_customer' => 'required',
      'email_customer' => 'required',
      'city_customer' => 'required',
    ]);
    $customer = Customer::create($request->all());
  }
  /**
   * Display the specified resource.
   *
   * @param  \App\Models\Customer  $customer
   * @return \Illuminate\Http\Response
   */
  public function show(Customer $customer) {
    return new CustomerResource($customer);
  }
  /**
   * Show the form for editing the specified resource.
   *
   * @param  \App\Models\Customer  $customer
   * @return \Illuminate\Http\Response
   */
  public function edit(Customer $customer) {
    //
  }
  /**
   * Update the specified resource in storage.
   *
   * @param  \Illuminate\Http\Request  $request
   * @param  \App\Models\Customer  $customer
   * @return \Illuminate\Http\Response
   */
  public function update(Request $request, Customer $customer) {
    $customer->update($request->all());
  }
  /**
   * Remove the specified resource from storage.
   *
   * @param  \App\Models\Customer  $customer
   * @return \Illuminate\Http\Response
   */
  public function destroy(Customer $customer) {
    $customer->delete();
  }
}
```

C:\xampp\htdocs\api\app\Http\Controllers\Api\V2\CustomerController2.php

```
<?php
namespace App\Http\Controllers\Api\V2;
use App\Http\Resources\CustomerCollection;
use App\Http\Resources\V2\CustomerResource;
use App\Models\Customer;
use Illuminate\Http\Request;
class CustomerController2 extends Controller {
  /**
   * Display a listing of the resource.
   *
   * @return \Illuminate\Http\Response
   */
  public function index() {
    // return Customer::all();
    // return CustomerResource::collection(Customer::paginate());
    // return new CustomerCollection(Customer::paginate());
    return CustomerResource::collection(Customer::paginate());
  }
  /**
   * Show the form for creating a new resource.
   *
   * @return \Illuminate\Http\Response
   */
  public function create() {
    //
  }
  /**
   * Store a newly created resource in storage.
   *
   * @param  \Illuminate\Http\Request  $request
   * @return \Illuminate\Http\Response
   */
  public function store(Request $request) {
    $request->validate([
      'name_customer'    => 'required',
      'phone_customer'   => 'required',
      'address_customer' => 'required',
      'email_customer'   => 'required',
      'city_customer'    => 'required',
    ]);
    $customer = Customer::create($request->all());
  }
  /**
   * Display the specified resource.
   *
   * @param  \App\Models\Customer  $customer
   * @return \Illuminate\Http\Response
   */
  public function show(Customer $customer) {
    return new CustomerResource($customer);
  }
  /**
   * Show the form for editing the specified resource.
   *
   * @param  \App\Models\Customer  $customer
   * @return \Illuminate\Http\Response
   */
  public function edit(Customer $customer) {
    //
  }
  /**
   * Update the specified resource in storage.
   *
   * @param  \Illuminate\Http\Request  $request
   * @param  \App\Models\Customer  $customer
   * @return \Illuminate\Http\Response
   */
  public function update(Request $request, Customer $customer) {
    $customer->update($request->all());
  }
  /**
   * Remove the specified resource from storage.
   *
   * @param  \App\Models\Customer  $customer
   * @return \Illuminate\Http\Response
   */
  public function destroy(Customer $customer) {
    $customer->delete();
  }
}
```

C:\xampp\htdocs\api\routes\api.php

```
<?php
use App\Http\Controllers\Api\V1\CustomerController1;
use App\Http\Controllers\Api\V2\CustomerController2;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
 */
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
  return $request->user();
});
// Route::resource('customers', CustomerController::class)->only(['index','show','destroy','update','store']);
// Route::resource('customers', CustomerController::class)->except(['create', 'edit']);
// Route::resource('customers', CustomerController::class)->except(['create','edit']);
Route::prefix('v1')->group(function () {
  Route::resource('customers', CustomerController1::class)->except(['create', 'edit']);
});
Route::prefix('v2')->group(function () {
  Route::resource('customers', CustomerController2::class)->except(['create', 'edit']);
});
```
