getting_started_with_laravel

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revisionBoth sides next revision
getting_started_with_laravel [03.12.2020 15:43] – [Form validation] Pascal Sutergetting_started_with_laravel [04.12.2020 11:19] – [dump / dump and die] Pascal Suter
Line 169: Line 169:
   * in addition to bringing the user back to the form, laravel will provide an ''$error'' variable which can be used in the balde    * in addition to bringing the user back to the form, laravel will provide an ''$error'' variable which can be used in the balde 
   * even better, there is a ''@error(fieldname)'' blade syntax which will do whatever follows after it if the specified field is invalid   * even better, there is a ''@error(fieldname)'' blade syntax which will do whatever follows after it if the specified field is invalid
- +  * you also get a ''old(fieldname)'' function which you can use to populate your input fields with the original inputs, so the user won't have to enter eveything again.. see the eample below.  
-here's an example part of a blade with a form that will show some info if the validation fails: +here's an example part of a (bootstrap based) blade with a form that will show some info if the validation fails: 
  
 <code html> <code html>
Line 183: Line 183:
       <div class="form-group col-12">       <div class="form-group col-12">
         <label for="inputName">Name</label>         <label for="inputName">Name</label>
-        <input type="text" class="form-control @error('name') is-invalid @enderror" id="inputName" name="name">+        <input type="text" class="form-control @error('name') is-invalid @enderror" id="inputName" name="name" value="{{ old('name') }}">
         <div class="invalid-feedback">Please enter your name here</div>         <div class="invalid-feedback">Please enter your name here</div>
       </div>       </div>
Line 190: Line 190:
  
 ===== Tricks ===== ===== Tricks =====
 +==== dump / dump and die ====
 +this has to be the first tip :) .. there is a helper function called ''dd()'' which stands for ''dump and die''. it dumps a variable and then stops further execution of the script. this is of course extremely helpful when learning stuff and trying out things. 
 +
 +you may not always want to stop the execution, so another helper called ''dump()'' comes in handy here. it produces the same output but contines the execution of your script 
 +
 +to use those helpers in a blade, there are special blade syntax helpers available which make it even easier: 
 +<code>@dump($__data)</code>
 +for example dumps all defined variables. there is also a ''@dd()'' helper available. 
 +
 +note that using <code>{{ dump($__data) }}</code> won't work. there is a detailed explanation why [[https://stackoverflow.com/questions/48731507/laravel-dump-unexpected-output|on stackexchange]]
 ==== 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 224: Line 234:
 @extends('layouts.mylaout', [ 'title' => "Details for ".$section->name, 'image' => 'section_details.jpg' ]) @extends('layouts.mylaout', [ 'title' => "Details for ".$section->name, 'image' => 'section_details.jpg' ])
 </code> </code>
-and in the layout you would then simply use ''{{ $title }}'' to echo the title you passed along. +and in the layout you would then simply use <code>{{ $title }}</code> to echo the title you passed along. 
  
 === 2.) create a mini-section === === 2.) create a mini-section ===
  • getting_started_with_laravel.txt
  • Last modified: 04.12.2020 11:44
  • by Pascal Suter