Create Version API (ok)
PreviousFaker Images, numberBetween(1, 20) in Laravel, migration (ok)Next[VUEJS] Phân trang pagination full (ok)
Last updated
Last updated
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']);
});