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 [20.11.2020 16:44] – [saving JSON in a MySQL table] Pascal Suter | getting_started_with_laravel [03.12.2020 15:46] – [passing variables from content blades to layouts] Pascal Suter | ||
---|---|---|---|
Line 8: | Line 8: | ||
as with most successful programming languages and frameworks, Laravel seems to provide a good documentation. just go to the [[https:// | as with most successful programming languages and frameworks, Laravel seems to provide a good documentation. just go to the [[https:// | ||
- | ====== Laravel installation (on Ubuntu 20.04 with Apache2) | + | ===== Laravel installation (on Ubuntu 20.04 with Apache2) ===== |
I don't care or like the included web-server in laravel for development.. it isn't that much work to set up a proper webserver like you would have on your productive environment, | I don't care or like the included web-server in laravel for development.. it isn't that much work to set up a proper webserver like you would have on your productive environment, | ||
Line 77: | Line 77: | ||
- | ====== Basics | + | ===== Basics ===== |
Laravel follows the Model View Controller design principle.. where the View is basically your GUI, the Webpage in this case, the Model handles storing and reading your data.. so that's your database and some PHP scripts to access the database and retrieve Entries from it, and the Controller is the glue between the two, that's where your actual program logic happens.. the controller does something with the data and passes the result to the View. | Laravel follows the Model View Controller design principle.. where the View is basically your GUI, the Webpage in this case, the Model handles storing and reading your data.. so that's your database and some PHP scripts to access the database and retrieve Entries from it, and the Controller is the glue between the two, that's where your actual program logic happens.. the controller does something with the data and passes the result to the View. | ||
Line 103: | Line 103: | ||
**processing delete requests** are like posts, but we can't set the form method to '' | **processing delete requests** are like posts, but we can't set the form method to '' | ||
- | ====== Naming Conventions | + | |
+ | ===== Relations ===== | ||
+ | relations are defined in the Model by adding a new public function to it. | ||
+ | |||
+ | of course the migration must also contain the necessary fields to build the relations. the convention is to use something like | ||
+ | <code php> | ||
+ | $table-> | ||
+ | </ | ||
+ | which will automatically define a foreing ID in the current table that is linked to the '' | ||
+ | |||
+ | so here is how various relationships are defined in the model for the respective table(s) | ||
+ | ==== 1:1 relationships ==== | ||
+ | here's an example.. the '' | ||
+ | |||
+ | in the **Assessment Model** we write: | ||
+ | <code php> | ||
+ | public function job(){ | ||
+ | return $this-> | ||
+ | } | ||
+ | </ | ||
+ | and vice versa, in the **Job Model** we write: | ||
+ | <code php> | ||
+ | public function assessment(){ | ||
+ | return $this-> | ||
+ | } | ||
+ | </ | ||
+ | now we can simply access the job via the assessment by using something like '' | ||
+ | |||
+ | |||
+ | ===== Naming Conventions ===== | ||
I'm going to use the examples of the above mentioned YouTube Series here, which is all about a pizza place :) | I'm going to use the examples of the above mentioned YouTube Series here, which is all about a pizza place :) | ||
* Model Name: Pizza | * Model Name: Pizza | ||
Line 120: | Line 149: | ||
- | ====== | + | ===== Form validation |
- | ===== Create Model + Controller + Migration all in one step ===== | + | Laravel provides a nice Method for validating forms. Typically the validator is added in the Controller '' |
+ | <code php> | ||
+ | public function store(Request $request){ | ||
+ | $validData | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ]); | ||
+ | // 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 '' | ||
+ | |||
+ | 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 ===== | ||
+ | ==== Create Model + Controller + Migration all in one step ==== | ||
artisan make:model Pizza -mc | artisan make:model Pizza -mc | ||
this creates the model, **m**igration and **c**ontroller all at once and puts names according to the above conventions in place. | this creates the model, **m**igration and **c**ontroller all at once and puts names according to the above conventions in place. | ||
- | ===== saving JSON in a MySQL table ===== | + | ==== saving JSON in a MySQL table ==== |
a newer feature of MySQL is the ability to store a complete dataset into a table field, sort of like NoSQL style databases do. | a newer feature of MySQL is the ability to store a complete dataset into a table field, sort of like NoSQL style databases do. | ||
**Migration** | **Migration** | ||
Line 139: | Line 208: | ||
this will convert JSON to an array when reading from the database and it will automatically do the oposite conversion when writing into the database. | this will convert JSON to an array when reading from the database and it will automatically do the oposite conversion when writing into the database. | ||
- | ===== using bootstrap 4 with laravel 8 ===== | + | ==== using bootstrap 4 with laravel 8 ==== |
by default laravel 8 uses Tailwind was their CSS framework. If you want to use Bootstarp however, you can. you can install it like so: | by default laravel 8 uses Tailwind was their CSS framework. If you want to use Bootstarp however, you can. you can install it like so: | ||
- you will need to install NPM and node through one of the many channels on your system | - you will need to install NPM and node through one of the many channels on your system | ||
Line 145: | Line 214: | ||
- install bootstrap through artisan (add the '' | - install bootstrap through artisan (add the '' | ||
- follow the onscreen instructions and run the necessary npm instructions < | - follow the onscreen instructions and run the necessary npm instructions < | ||
+ | |||
+ | ==== passing variables from content blades to layouts ==== | ||
+ | if you have your header saved to a layout which is '' | ||
+ | |||
+ | So far i've found two ways to do that.. | ||
+ | === 1.) pass arguments through the @extends function === | ||
+ | the '' | ||
+ | < | ||
+ | @extends(' | ||
+ | </ | ||
+ | and in the layout you would then simply use < | ||
+ | |||
+ | === 2.) create a mini-section === | ||
+ | you could also use sections for this, create a mini section which just contains the title string in your main content blade: | ||
+ | < | ||
+ | @section(' | ||
+ | @section(' | ||
+ | </ | ||
+ | and in your layout you would then use '' | ||
+ | |||
+ | honestly i haven' | ||
- | ====== Examples | + | ===== Examples ===== |
These examples are all taken from the [[https:// | These examples are all taken from the [[https:// | ||
- | ===== routes | + | ==== routes ==== |
<code php> | <code php> | ||
Route:: | Route:: | ||
Line 158: | Line 248: | ||
Route:: | Route:: | ||
</ | </ | ||
- | ===== controller | + | ==== controller ==== |
<code php> | <code php> | ||
public function index() { | public function index() { |