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
Next revisionBoth sides next revision
getting_started_with_laravel [03.12.2020 15:39] – [Form validation] Pascal Sutergetting_started_with_laravel [03.12.2020 15:45] – [passing variables from content blades to layouts] Pascal Suter
Line 153: Line 153:
 <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 162:
 </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 168: Line 170:
   * 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
  
-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 222: Line 224:
 @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 ''{ { $title } }'' 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