Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revisionBoth sides next revision | ||
getting_started_with_laravel [03.12.2020 15:29] – Pascal Suter | getting_started_with_laravel [04.12.2020 11:16] – [Create Model + Controller + Migration all in one step] Pascal Suter | ||
---|---|---|---|
Line 148: | Line 148: | ||
* foreign keys in should be named in the singular, lowercase and underscored form of the target table plus '' | * foreign keys in should be named in the singular, lowercase and underscored form of the target table plus '' | ||
+ | |||
+ | ===== Form validation ===== | ||
+ | Laravel provides a nice Method for validating forms. Typically the validator is added in the Controller '' | ||
+ | <code php> | ||
+ | public function store(Request $request){ | ||
+ | $validData = $request-> | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ]); | ||
+ | // anything from here on will only be executed if the above validation was successful | ||
+ | } | ||
+ | </ | ||
+ | there are many [[https:// | ||
+ | |||
+ | also note, that the '' | ||
+ | |||
+ | now here are a few cool things about the validator: | ||
+ | * if it finds an invalid field, it will stop right there and laravel will show you the form page again without any further routes needed. | ||
+ | * in addition to bringing the user back to the form, laravel will provide an '' | ||
+ | * even better, there is a '' | ||
+ | * you also get a '' | ||
+ | here's an example part of a (bootstrap based) blade with a form that will show some info if the validation fails: | ||
+ | |||
+ | <code html> | ||
+ | @if ($errors-> | ||
+ | <div class=" | ||
+ | You did not fill in all the form fields correctly, please correct or complete the Information given below | ||
+ | </ | ||
+ | @endif | ||
+ | <form action=" | ||
+ | @csrf | ||
+ | <div class=" | ||
+ | <div class=" | ||
+ | <label for=" | ||
+ | <input type=" | ||
+ | <div class=" | ||
+ | </ | ||
+ | .... | ||
+ | </ | ||
===== Tricks ===== | ===== Tricks ===== | ||
+ | ==== dump / dump and die ==== | ||
+ | this has to be the first tip :) .. there is a helper function called '' | ||
+ | |||
+ | you may not always want to stop the execution, so another helper called '' | ||
+ | |||
+ | the latter is especially helpful when dumping variables in a blade. however, if you use | ||
+ | < | ||
+ | {{ dump($__data) }} | ||
+ | </ | ||
+ | for example to get a dump of all defined variables in your blade, you are going to run into a laravel error which tells you that htmlspecialchars only works with strings but got something else (an array most likely).. | ||
+ | |||
+ | so to use dump in a blade, you can use a blade syntax '' | ||
==== Create Model + Controller + Migration all in one step ==== | ==== Create Model + Controller + Migration all in one step ==== | ||
artisan make:model Pizza -mc | artisan make:model Pizza -mc | ||
Line 184: | Line 236: | ||
@extends(' | @extends(' | ||
</ | </ | ||
- | and in the layout you would then simply use '' | + | and in the layout you would then simply use < |
=== 2.) create a mini-section === | === 2.) create a mini-section === |