Note: update the laravel docs references to the currently used version (see "laravel/framework" in composer.json
)
The basic things you will need as a beginner:
- Controllers:
app/Http/Controllers
- the main functionalities of the backend - Database:
- Migrations:
database/migrations
- the database structure can be modified with migrations - Seeders:
database/seeders
,database/factories
- the database can be seeded with dummy data which are set in factories - Read the documentation of queries to understand how to insert/update/delete data in the database. Laravel also has particular functions for inserting and updating models. The queries will mostly return Collections, which are similar to arrays but with custom functions.
- Migrations:
- Models:
app/Models
- the database is mapped to php classes using Models (ORM - Object Relational Mapping). Models are the main way to interact with the database. To create Models, usephp artisan make:model ModelName -a
to generate the model and a corresponding controller, factory, etc. Also take a look at Relationships to define relations between Models. - Routes:
routes/web
- to map the requests from the browser to controller functions - Frontend:
resources/views
- the webpage is generated from these blade files. To return a webpage, usereturn view('path.to.view', ['additional_data' => $data, ...]);
. Blade files are html codes with additional php functionalities: loops, variables, etc. Writing{{ something }}
is equivalent to php's print:echo something;
. When writing forms, add@csrf
to the form for security reasons (without it, the request will not work). Blade files can also be nested, included, etc (eg.@include('path.to.file'))
). Our front-end framework is Materialize. - Language files:
resources/lang
- to translate the webpage. Use__('filename.key')
in the backend and@lang('filename.key')
in blades. To add variables:__('filename.key', ['variable' => 'value'])
, prefix the variable name with:
in the language files. We translate to English and Hungarian, other languages are handled by the localization contribution model. - Validation: It is recommended to validate every user input: for example, in the controller:
$request->validate(['title' => 'required|string|max:255']);
. Available validation rules. - Debugging: log files:
storage/logs/laravel.log
- useLog::info('something happened', ['additional_data' => $data])
to log (also: error, warning, etc.). Alternatively, in the controllers, you can typereturn response()->json(...);
to print something in the browser. In blades, type{{ var_dump($data) }}
to display some data. It is worth to take a look at the query debugging options also.
You can use php artisan db
command to enter into the database directly or php artisan tinker
to get a runtime laravel evaluation tool.