Tasks studies - laboratory
• In XAMPP, start Apache and MySQL, then go to phpMyAdmin,
• Download the Lab007_AI1_start.zip archive to your desktop, which contains the startup project for performing tasks, and unpack this archive,
• Go to the unpacked folder and, if you have settings other than the default ones (e.g. database connection), change them in .env.example
and start.…,
• Run the start.bat script (Windows, double-click) or start.sh (other systems, via the bash command start.sh
).
Explain the following topics:
• CRUD operations,
• data validation,
• data validation on the backend side,
• data validation on the frontend side,
• redirection,
• redirection to a named path in Laravel.
Open the cmd terminal (Command Prompt) in VSCode. Start the php development server for using the artisan serve command.
php artisan serve
In your web browser, go to: http://localhost:8000/trips
Run the following command to generate a new controller for the
Country model, which is a “resource” along with classes to set validation rules for selected
request types (app/Http/Requests/…
).
[link](https://laravel.com/docs/11.x/controllers#resource-controllers
[link](https://laravel.com/docs/11.x/controllers#generating-form-requests
php artisan make:controller CountryController --model=Country --resource --requests
Set routes for controller app/Http/Controllers/CountryController.php
regarding
CRUD
operations on resource Country.
Display currently configured routing.
[link](https://laravel.com/docs/11.x/controllers#generating-form-requests
Route::resource('countries', CountryController::class);
php artisan route:list
GET|HEAD countries ......................... countries.index › CountryController@index
POST countries ......................... countries.store › CountryController@store
GET|HEAD countries/create ......... countries.create › CountryController@create
GET|HEAD countries/{country} ................... countries.show › CountryController@show
PUT|PATCH countries/{country} ......... countries.update › CountryController@update
DELETE countries/{country} ......... countries.destroy › CountryController@destroy
GET|HEAD countries/{country}/edit .............. countries.edit › CountryController@edit
Supplement the index function in CountryController to return a view from the file
resources\views\countries\index.blade.php
along with passing to it the variable
countries containing a collection of all countries retrieved from the database.
Then, in this view, complete the generation of an HTML table with countries based on the contents of the countries collection.
In your web browser, go to: http://localhost:8000/countries
Supplement the create function in CountryController to return a view from the file resources\views\countries\create.blade.php.
Then, in this view, supplement the form with the missing field, in order to be able to
enter all the data of the new country and send it via POST request to the address
handled by the store function.
In a web browser, go to: http://localhost:8000/countries/create
Supplement the store function in CountryController
to add a new country to the database,
with data collected from the form in the previous task.
After successful insertion of the country into the database → redirection to country.index
should occur,
after failure → default action.
In app\Http\Requests\StoreCountryRequest.php
:
• in authorize change false to true,
• in rules propose and supplement the rules for validating new countries, e.g. optionality, data type, unique names, max. name length, min/max area range.
Check the operation of the form for adding a new country:
• by providing the country with correct data,
• by providing the country with incorrect data, e.g. missing values, negative area.
In resources\views\countries\index.blade.php
modify the table so that it has an
additional column containing an Edit
link in each row.
<td><a href="">Edit</a></td>
or
<td><a href="">Edit</a></td>
Supplement the edit function in CountryController to return a view from the file
resources\views\countries\edit.blade.php
along with passing to it the country data
automatically mapped and downloaded from the database, selected for editing (in order to
pre-fill the form fields with the data of this country).
Then, fill in the form to edit the data of this country in order to send it with a request
POST
(with a hidden _method field with the value PUT
– specific to Laravel) to the address
supported by the update function, passing the country id or the entire country object.
In a web browser, go to the address: http://localhost:8000/countries/1/edit
Complete the update function in CountryController
to update the database of a specific
country with the data accepted in the request.
After successful country editing → redirection to country.index should occur,
after failure → default action.
In app\Http\Requests\UpdateCountryRequest.php
:
• in authorize change false to true,
• in rules propose and complete validation rules for new countries, e.g. optionality, data type, unique names, max. name length, min/max area range.
Include exclusion of uniqueness checking for the same object.
public function update(UpdateCountryRequest $request, Country $country)
{
$input = $request->all();
$country->update($input);
return redirect()->route('countries.index');
}
https://www.csrhymes.com/2019/06/22/using-the-unique-validation-rule-in-laravel-form-request.html
return [
'name' => 'required|unique:countries,name,'.$this->country->id.'|max:50',
//...,
];
Check the operation of the edit form for a given country:
• by providing correct data,
• by providing incorrect data, e.g. missing values, negative area.
Supplement the destroy function in CountryController
to remove a specific
selected country from the database.
After successful deletion of the country → redirection to country.index
should take place,
after failure → default action.
In resources\views\countries\index.blade.php
modify the table so that it contains
an additional column containing a (small) form with a Delete button regarding deleting
a given country in each row.
The form should be sent as a POST
request (with a hidden _method field with the value
DELETE
– specific to Laravel) to the address handled by the destroy function
with the country id passed.
<td>
<form method="POST" action="">
@csrf
@method('DELETE')
<input type="submit" class="btn btn-danger" value="Delete"
style="--bs-btn-padding-y: .25rem; --bs-btn-padding-x: .5rem; --bs-btn-font-size: .75rem;" />
</form>
</td>
Check the operation of deleting a given country:
• by deleting a country from the table,
• by deleting a country from the table with a non-existent identifier modified in the developer tools (F12):
Try to delete the first country. Explain the situation. Propose a solution
to the problem.
Use the method of displaying errors in the session placed in the template
resources\views\shared\session-error.blade.php
.
In resources\views\countries\index.blade.php
modify the table so that in the column
containing country ids they are links, and clicking would take you to a subpage
with information about a given country (using the show function and the show.blade.php view).
Design a subpage in this view, e.g. card/table, etc.
↓
Modify the navbar so that it has two links:
• to the main page of the trips,
• to the table with countries.
Using the request object, depending on the page being viewed, highlight the appropriate link.
Add two new functions to TripController
: edit and update, in which similarly
program the editing of a given trip. Set routing for them. The form should have a select field for user-friendly selection of the country (based on the countries currently available in the database) where the trip is taking place.
Include template attachments shared.session-error
and shared.validation-error
.
Route::get('/trips/{id}/edit', 'edit')->name('trips.edit');
Route::put('/trips/{id}', 'update')->name('trips.update');
Add: rules to the previous task validations and their handling, but this time in a different way: directly defining them in the update function (instead of …TripRequest classes) along with passing them to validate.
http://localhost:8000/trips
$request->validate([
'name' => 'required|string|unique:trips,name,'.$id',
//...,
]);
In resources\views\trips\index.blade.php
modify the table so that it has
an additional column containing an “Edit” link in each row, leading to
the edit subpage of the given trip.
– tasks/sub-items to complete/perform on your own,
– tasks/sub-items for those interested.
To pack the project, use the script archiwizacja.bat. After unpacking the project, use start.bat again.
File version: v1.0