Tasks studies - laboratory
• in XAMPP start Apache and MySQL
, then go to phpMyAdmin,
• download the Lab006_AI1_start.zip
archive to the desktop, which contains the starting 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 connections), change them in the .env.example
file,
• run the start.bat
script (Windows, double-click) or start.sh
(other systems,
via the bash start.sh
command).
Explain the operation of commands contained in the start.bat
file.
Open the cmd terminal (Command Prompt)
in VSCode.
Start the php development server using the serve artisan command.
php artisan serve
In your web browser, go to:
http://localhost:8000
Familiarize yourself with the following topics:
• models (app/Models
),
• migrations (database/migrations
),
• seeders (database/seeders
),
• naming conventions accepted by the Laravel community.
Find files in the project regarding the above issues regarding the Country model.
Open the second tab of the cmd terminal (Command Prompt
) in VSCode.
Generate a new tour model and generate for it: migrations, seeder and controller.
php artisan make:model Trip -msc
Based on the requirements from the last task of the previous lab and
taking into account its example solution available in the trips.sql file complete
the migration (database/migrations/…_create_trips_table.php
) creating the trips table
with the required columns and their data types.
Then run the migration and check the database content in phpMyAdmin (refresh it after each execution).
If you notice a mistake, undo the last migration, make a correction and finally rerun the migration. Alternatively, you can use fresh.
php artisan migrate
php artisan migrate:rollback
php artisan migrate:fresh
Based on the example solution available in the trips.sql
file,
a seeder for countries was prepared (the file database/seeders/CountrySeeder.php
).
Also, database/seeders/TripSeeder.php
should be completed so that sample data is added to the trips table (for now the first two trips, then all *).
Refer to the contents of the app/Models/Country.php file. Complete the app/Models/Trip.php trip model with the following:
• the trip table should contain the created_at and updated_at columns,
• the default value of days for the trip is 7,
• set all properties as fillable except table ids,
• implement the association between country and trip models (both sides).
link $timestamps
link $attributes
link $fillable
Perform a seed and check the database content in phpMyAdmin
php artisan db:seed --class=CountrySeeder
php artisan db:seed --class=TripSeeder
Complete the database/seeders/DatabaseSeeder.php
file so that the country and trip seeders are executed in the correct order, when entering a single command.
php artisan db:seed
Supplement app/Http/Controllers/TripController.php
with two public functions:
• index function, returning a view from the file resources/views/trips/index.blade.php
together
with passing to it the trips variable containing a collection of all trips
downloaded from the database,
• show
function, accepting one id parameter, returning a view from the file resources/views/
trips/show.blade.php
with passing to it one trip object downloaded
from the database (selection based on the id parameter value) or status 404 in case,
when the trip does not exist.
public function index()
{
return /_ … _/;
}
public function show($id)
{
return /_ … _/;
}
Set routes for both controller functions from the previous task.
For the second function, you need to include id
(parameter in the path). Name this route (named
route, i.e. “named route”) as: trips.show
.
Using the contents of the trips
collection (passed to the view), perform
dynamic generation of cards and tables regarding trips using the @forels
e … @empty
… @endforelse loop.
In the “More details…” link, set the href as a reference to the route named in the previous task, along with the id of the individual trip.
Using the trip object passed to the show.blade.php
view:
• place a card with the details of this trip on the page,
• remove the “More details…” link,
• center the card,
• center the “Trip” header,
• change the page title to “Trip …” (name of the given trip).
Go to the following addresses:
http://localhost:8000/trips/1
http://localhost:8000/trips/10
Check the “More details…” links on the tabs on the page:
http://localhost:8000/trips
Task 6.14: *](https://github.com/dawidolko/Web-Applications/tree/main/LAB06/task)
Replace the routes for both controller functions from task 6.10
with one group of routes for this
controller. Do not omit the name of the “route named” trips.show
.
Propose a solution to the problem of the excessive number of cards on the page, e.g.:
• display 4 cheapest trips,
• display 4 random trips,
• change the layout of the cards display.
Use the archiwizacja.bat
script to pack the project and other files
excluded in .gitignore
(mainly bypassing the vendor
folder).
Try using SQLite instead of MySQL:
• in .env change DB_CONNECTION
to sqlite,
• perform migrations and seeders,
• check the contents of the newly created database in the ai1_lab6
file (in the main directory
of the project) using e.g. DBeaver
.
php artisan migrate:fresh --seed
Database -> New Database Connection -> SQLite -> path to file -> Finish
– tasks/sub-items to complete/perform on your own,
– tasks/sub-items for those interested.
File version: v1.0