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
getting_started_with_laravel [03.12.2020 15:39] – [Form validation] Pascal Sutergetting_started_with_laravel [04.12.2020 11:44] (current) – [Basics] Pascal Suter
Line 104: Line 104:
 **processing delete requests** are like posts, but we can't set the form method to ''DELETE'' so we need to use a blade function ''@method('DELETE')'' just after the form tag.  **processing delete requests** are like posts, but we can't set the form method to ''DELETE'' so we need to use a blade function ''@method('DELETE')'' just after the form tag. 
  
 +**sessions and flashing data** in laravel we can either store data into our session, like in normal php.. we can do this either via the ''session()'' helper which will save new key=>value pairs when it is passed an array as argument like ''session(['newkey',$someVar])'' for example. to get ''$someVar'' back we would use the helper like ''$someVar=session('newkey')''. alternatively, we can use an instance of the ''Request'' class and the ''put()'' and ''get()'' functions for the ''session'' property like so: ''$request->session()->put('tmpfiles',$tmpfiles);'' and vice versa ''$tmpfiles = $request->session()->get('tmpfiles',[]);'' notice a nice feature of the ''get()'' method: we can pass a default value as a second argument which will be returned in case the session variable is not set. that's pretty helpful. Session variables are also available in blades by using the ''session()'' helper like above. <br>
 +a specialty in laravel is, that we can **flash** data to a session. flashing means, that the data will only remain in the session for the next request. after that it will either be unset or we need to **reflash** to keep the flashed variables. to flash a variable to a session, we do the same as above with the ''get()'' function but now use a ''flash()'' method, that's it. 
 ===== Relations ===== ===== Relations =====
 relations are defined in the Model by adding a new public function to it.  relations are defined in the Model by adding a new public function to it. 
Line 153: Line 155:
 <code php> <code php>
     public function store(Request $request){     public function store(Request $request){
-        $validateData = $request->validate([+        $validData = $request->validate([
             'name' => ['required', 'string', 'max:255'],             'name' => ['required', 'string', 'max:255'],
             'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],             'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
Line 162: Line 164:
 </code> </code>
 there are many [[https://laravel.com/docs/8.x/validation#available-validation-rules|validation rules]] available already in Laravel and custom rules can be added if needed.  there are many [[https://laravel.com/docs/8.x/validation#available-validation-rules|validation rules]] available already in Laravel and custom rules can be added if needed. 
 +
 +also note, that the ''$validData'' variable now contains an array with the validated data in a format that is compatible with the ''create'' methods for the laravel models. So if you set up your model's ''$fillable'' property correctly and your form field names correspond to your database field names, you can pass this array straight to the ''create()'' method of your model and create a new database entry like that!
  
 now here are a few cool things about the validator:  now here are a few cool things about the validator: 
Line 167: Line 171:
   * 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 181: Line 185:
       <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 188: Line 192:
  
 ===== 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 222: Line 236:
 @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.1607006395.txt.gz
  • Last modified: 03.12.2020 15:39
  • by Pascal Suter