Tasks studies - laboratory
• In XAMPP, start Apache and MySQL, then go to phpMyAdmin,
• Download the Lab009_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 connections), change them in .env.example and start.…,
• Run the start.bat
script (Windows, double-click) or start.sh
(other systems,
using the bash start.sh
command).
Explain the following issues:
• N+1
problem,
• Eloquent ORM, two modes of loading data related by relationships,
• which causes and which prevents the N+1
problem,
• solution to the N+1
problem.
Open the cmd terminal (Command Prompt) in VSCode.
Start the php development server for using the serve artisan
command.
php artisan serve
Open the second tab of the cmd terminal (Command Prompt) in VSCode.
Install the Laravel Debugbar package and check its operation.
composer require barryvdh/laravel-debugbar --dev
Go to the tours subpage.
Go to the Queries
tab in the debugbar.
Explain why 8 queries are executed (1 + 7).
In a web browser, go to:
http://localhost:8000/auth/login
Implement the N+1 problem fix by changing the loading of the collection of trips with countries in the index function in TripController
from “lazy” to “greedy”.
Go back to the trips subpage. Observe the difference in the structure of the queries.
Additionally, limit the download of only the necessary columns regarding countries.
Observe the difference again.
Read about other examples of the N+1
problem.
Explain the following topics:
• business logic,
• service layer (use, advantages),
• dependency injection.
(in case of reaching the limit of articles from this portal, open links in a private window)
The article also has a layer of “repositories”, but instead of them, you can add new functions in models (related to a given model), which can then be used in services.
In TripController add a constructor in which to place the “injection” of the “TripService” service along with the assignment to the tripService class variable (property).
protected $tripService;
public function __construct(TripService $tripService)
{
$this->tripService = $tripService;
}
In TripService
, complete the calculatePromoPrice() function, in which, based on the value of the
price parameter (current price of the trip), calculate the new promotional price
of the trip:
• if the current price is less than PLN 5,000, then the promotion does not occur,
• if the current price is less than PLN 10,000, then reduce the price by PLN 2,500,
• in the remaining case, reduce the price by PLN 5,000.
Return the new price of the trip.
In TripController
in the show()
function call the calculatePromoPrice()
function to obtain
the new price of the trip, which you then pass to the show view as the
promoPrice variable.
Displaying the new price in the card has already been prepared.
Check the price calculations for different trips.
Familiarize yourself with the following issues:
• logs – logging,
• channels,
• log levels,
• current settings in the config/logging.php file,
• middleware.
Execute the following custom command (located in routes/console.php
).
php artisan log:clear
Add the following lines to the show function in TripController
.
Go to several subpages about the given trip.
Then check the contents at the end of the storage/logs/laravel.log
file, and the last
lines in the terminal (where the application is running).
use Illuminate\Support\Facades\Log;
Log::info('Showing details for trip: '.$id);
Log::channel('stderr')->info('Showing details for trip: '.$id);
http://localhost:8000/trips/1
http://localhost:8000/trips/4
http://localhost:8000/trips/6
Add a new lab9_errors channel in config/logging.php
in channels, which will be able to
save messages about errors to the lab9_errors.log
file.
Go to bootstrap/app.php and change “error/exception reporting” from std_err
to the channel created in the previous task. Then:
• log in as user Jan and try to remove the country USA,
• disable the MySQL database (in the XAMPP panel) and perform any action.
After that, check the contents of lab9_errors.log
. Enable the MySQL database.
Using the command below, create a new middleware called LogActivity.
In bootstrap/app.php
, register the LogActivity middleware for the web group.
link (no Http/Kernel.php
file from Laravel 10)
php artisan make:middleware LogActivity
->withMiddleware(function (Middleware $middleware) {
$middleware->web(LogActivity::class);
})
Go to app/http/Middleware/LogActivity.php
and complete the handle function so that it is
“user activity monitored”:
• the terminal log should show general information that someone has made a request from a given HTTP method on a given path (general form),
• the log in the laravel.log
file should contain information about which user
exactly made the request with a given method on a given path (specific form with
parameter values),
• if the user is not logged in, the log in the laravel.log
file should contain, for example,
anonymous instead of the user’s email.
public function handle(Request $request, Closure $next): Response
{
Log::channel('stderr')->notice(
"Someone did {$request->method()} on '{$request->route()->uri()}'"
);
$email = $request->user() ? $request->user()->email : 'anonymous'; Log::channel('single')->notice(
"'{$email}' did {$request->method()} on '{$request->getRequestUri()}'"
);
return $next($request);
}
Check how LogActivity.php
works in practice by performing different actions while being logged in as different users.
Install the package with ready translations: laravel-lang
.
Change the locale in the config/app.php
file.
composer require laravel-lang/common --dev
php artisan lang:add pl
php artisan lang:update
'locale' => 'pl',
Log in to Jana. Go to edit any of the tours and test the validation (including the negative period), observe the current message translations.
Go to the lang/pl/validation.php
file and complete it so that the message regarding
the period is fully translated.
Go to the bootstrap/app.php file and move the message from line 29 as a value to
the lang/pl.json
file. Propose a key name.
In the lang/en.json
file, prepare the corresponding translation of the message to j.ang
.
Check the content of the message for the set locales: pl
and en
.
'locale' => 'en',
Similarly to the previous task, prepare a translation of the application title: “Mountain Trips” to “Mountain Trips” and use it in templates:
• head.blade.php
,
• navbar.blade.php
,
• footer.blade.php
.
Similarly, check for locale: pl
and en
.
<footer class="container-fluid bg-body-tertiary ">
<div class="row text-center pt-2">
<p>© — 2024</p>
</div>
</footer>
Light/dark theme - can be switched by clicking on the moon in the navbar, which using JS changes the value of the data-bs-theme attribute. Moon icon used from the Bootstrap icon set.
data-bs-theme="..."
Remembering favorite trips within a session - ability to like/unlike a trip by clicking a heart in a given row. After the session expires/is deleted, favorite trips are lost. Heart icons used from the Bootstrap icon set.
Error pages – for selected HTTP codes (statuses) you can create views, replacing the default view.
http://localhost:8000/trips/8
Upload of a trip image – a form accepting several images in the format .jpg
, .jpeg
and saving them to disk in storage.
Validation rules include validation for several files at once and contain their own messages.
After successfully saving the files, an information toast is displayed.
Transactions – …
– tasks/sub-items to complete/perform on your own,
– tasks/sub-items for those interested.
To pack the project, use the archiwizacja.bat
script. After unpacking
the project, use start.bat again.
File version: v1.0