Web applications

Tasks studies - laboratory


Project maintained by dawidolko Hosted on GitHub Pages — Theme by dawidolko

Web Applications 1 (AI1) – Lab 11

Laravel, REST API –

Implementing a CRUD Controller (Part 1)


Starting the Lab:


Tasks (Laravel)

Task 11.1

Understand the concepts from the last task of the previous lab.
Review the existing modifications:

(There are differences between Laravel 10.x and 11.x)

Commands:

php artisan install:api
composer remove laravel/sanctum
php artisan make:middleware ForceJsonResponse

Task 11.2

Open the cmd terminal in VSCode.
Start Laravel’s development server using:

php artisan serve

Task 11.3

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

Task 11.4

Set up routing for CountryController (since it is an API controller).
Modify routes/api.php and check configured routes.

Route::apiResource('countries', CountryController::class);

Expected Routes:

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

Task 11.5

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
}

lab11


Task 11.6

Implement CRUD functions manually, without using:

Future labs will introduce these features.


Task 11.7

Implement CountryController methods for successful cases.
Test using HTTP requests.

CRUD Implementation:

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);
}

Task 11.8

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);
}

Task 11.9

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);
}