Tasks studies - laboratory
Lab011_AI1_start.zip
to your desktop, which contains the starter project for the tasks, and extract the archive..env.example
and start...
files if you have different settings (e.g., database connection).start.bat
(Windows, double-click)start.sh
(other systems, run bash start.sh
).Understand the concepts from the last task of the previous lab.
Review the existing modifications:
api.php
file for API routes/endpoints.Accept: 'application/json'
to every request to prevent rendering views when errors occur.(There are differences between Laravel 10.x and 11.x)
Commands:
php artisan install:api
composer remove laravel/sanctum
php artisan make:middleware ForceJsonResponse
Open the cmd terminal in VSCode.
Start Laravel’s development server using:
php artisan serve
Open a second terminal tab in VSCode.
Run the following command to generate a new controller with CRUD operations.
This controller will exclude two unnecessary functions (explain why they are not needed).
php artisan make:controller CountryController --api --resource
Set up routing for CountryController
(since it is an API controller).
Modify routes/api.php
and check configured routes.
Route::apiResource('countries', CountryController::class);
GET|HEAD api/countries → countries.index › CountryController@index
POST api/countries → countries.store › CountryController@store
GET|HEAD api/countries/{country} → countries.show › CountryController@show
PUT|PATCH api/countries/{country} → countries.update › CountryController@update
DELETE api/countries/{country} → countries.destroy › CountryController@destroy
Implement the index
function in CountryController
to return all countries.
Test the result using:
public function index()
{
$countries = DB::table('countries')->get();
return $countries; // Status 200
}
Implement CRUD functions manually, without using:
Resource
, Collection
)StoreRequest
, UpdateRequest
)$country
)DB::...
)Future labs will introduce these features.
Implement CountryController
methods for successful cases.
Test using HTTP requests.
public function index()
{
return response()->json(DB::table('countries')->get(), 200);
}
public function show($id)
{
$country = DB::table('countries')->find($id);
return response()->json($country, 200);
}
public function store(Request $request)
{
$id = DB::table('countries')->insertGetId([
'name' => $request->input('name'),
'code' => $request->input('code'),
'currency' => $request->input('currency'),
'area' => $request->input('area'),
'language' => $request->input('language')
]);
$country = DB::table('countries')->find($id);
return response()->json($country, 201);
}
public function update(Request $request, $id)
{
DB::table('countries')->where('id', $id)->update([
'name' => $request->input('name'),
'code' => $request->input('code'),
'currency' => $request->input('currency'),
'area' => $request->input('area'),
'language' => $request->input('language')
]);
$country = DB::table('countries')->find($id);
return response()->json($country, 200);
}
public function destroy($id)
{
DB::table('countries')->where('id', $id)->delete();
return response()->json(null, 204);
}
Modify CountryController
to handle error cases (e.g., missing country, validation errors).
Example validation rules:
$rules = [
'name' => 'required|string|unique:countries,name|max:50',
'code' => 'required|string|unique:countries,code|max:3',
'currency' => 'required|string|max:30',
'area' => 'required|integer|min:0',
'language' => 'required|string|max:50',
];
$validator = Validator::make($inputs, $rules);
if ($validator->fails()) {
return response($validator->errors(), 422);
}
Modify response format by removing unnecessary fields:
public function index()
{
$countries = DB::table('countries')->get();
return response()->json($countries->map(function ($country) {
return [
'id' => $country->id,
'code' => $country->code,
'currency' => $country->currency,
'area' => $country->area,
'language' => $country->language
];
}), 200);
}