Web applications

Tasks studies - laboratory


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

Web Applications 1 (AI1) – Lab No. 6

Laravel – Eloquent ORM, migrations, seeds

Start of the lab:

• 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).

Tasks (Laravel):

Task 6.1: *

Explain the operation of commands contained in the start.bat file.

link

link

link

link

Task 6.2:

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

Task 6.3:

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.

link

link

link

link

php artisan make:model Trip -msc

Task 6.4:

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.

link

link

link

link

link

link

php artisan migrate
php artisan migrate:rollback
php artisan migrate:fresh

Task 6.5:

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 *).

link

link

link

Task 6.6:

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

link $timestamps

link $attributes

link $fillable

link

link

link

Task 6.7:

Perform a seed and check the database content in phpMyAdmin

php artisan db:seed --class=CountrySeeder
php artisan db:seed --class=TripSeeder

Task 6.8: *

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

Tasks (Blade, Eloquent ORM):

Task 6.9:

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.

link

link

link

public function index()
{
return /_ … _/;
}
public function show($id)
{
return /_ … _/;
}

Task 6.10:

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.

link

link

Task 6.11:

Using the contents of the trips collection (passed to the view), perform dynamic generation of cards and tables regarding trips using the @forelse … @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.




Task 6.12: *

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).

link

link

Go to the following addresses:

http://localhost:8000/trips/1

http://localhost:8000/trips/10

Task 6.13: *

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.

link

Task 6.15: *

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.

Task 6.16: *

Use the archiwizacja.bat script to pack the project and other files excluded in .gitignore (mainly bypassing the vendor folder).

Task 6.17: *

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.

link


php artisan migrate:fresh --seed


lab6

link

Database -> New Database Connection -> SQLite -> path to file -> Finish

File version: v1.0