/*
|-----------------------------------------------------------------------------
| Getting Started with Laravel 5.4
|-----------------------------------------------------------------------------
|
| Laravel 5.4 has more features and Laravel is a free, open-source PHP web
| framework, created by Taylor Otwell and intended for the development of
| web applications following the model–view–controller (MVC) architectural pattern.
|
*/
----------------------------
To create a migration, use the make:migration Artisan command:
Syntax:
php artisan make:migration create_users_table
The new migration will be placed in your database/migrations directory. Each migration file name contains a timestamp which allows Laravel to determine the order of the migrations.
Example:
vagrant@homestead:~/sample$ php artisan make:migration create_student_table
Created Migration: 2017_08_01_044007_create_student_table
--------------------
#Running Migrations
--------------------
To run all of your outstanding migrations, execute the migrate Artisan command:
Syntax:
php artisan migrate
If you are using the Homestead virtual machine, you should run this command from within your virtual machine.
To create a new database table, use the create method on the Schema facade. The create method accepts two arguments. The first is the name of the table, while the second is a Closure which receives a Blueprint object that may be used to define the new table:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
});
Of course, when creating the table, you may use any of the schema builder's column methods to define the table's columns.
------------------
To get started, let's create an Eloquent model. Models typically live in the app directory, but you are free to place them anywhere that can be auto-loaded according to your composer.json file. All Eloquent models extend Illuminate\Database\Eloquent\Model class.
The easiest way to create a model instance is using the make:model Artisan command:
Syntax:
php artisan make:model Models/Student
Example:
vagrant@homestead:~/sample$ php artisan make:model Models/Student
Model created successfully.
----------------------
Laravel resource routing assigns the typical "CRUD" routes to a controller with a single line of code. For example, you may wish to create a controller that handles all HTTP requests for "photos" stored by your application. Using the make:controller Artisan command, we can quickly create such a controller:
Syntax:
php artisan make:controller StudentController --resource
This command will generate a controller at app/Http/Controllers/StudentController.php. The controller will contain a method for each of the available resource operations.
Next, you may register a resourceful route to the controller:
Route::resource('Student', 'StudentController');
This single route declaration creates multiple routes to handle a variety of actions on the resource. The generated controller will already have methods stubbed for each of these actions, including notes informing you of the HTTP verbs and URIs they handle.
Example:
vagrant@homestead:~/sample$ php artisan make:controller StudentController --resource
Controller created successfully.
------------------------
#Creating Form Request
-----------------------
For more complex validation scenarios, you may wish to create a "form request". Form requests are custom request classes that contain validation logic. To create a form request class, use the make:request Artisan CLI command:
Syntax:
php artisan make:request StoreBlogPost
The generated class will be placed in the app/Http/Requests directory. If this directory does not exist, it will be created when you run the make:request command.
Example:
vagrant@homestead:~/student$ php artisan make:request StudentRequest
Request created successfully.
---------------
#Installation
--------------
Begin by installing this package through Composer. Edit your project's composer.json file to require laravelcollective/html.
Syntax:
composer require "laravelcollective/html":"^5.4.0"
Example :
vagrant@homestead:~/sample$ composer require "laravelcollective/html":"^5.4.0"
Next, add your new provider to the providers array of config/app.php:
'providers' => [
// ...
Collective\Html\HtmlServiceProvider::class,
// ...
],
Finally, add two class aliases to the aliases array of config/app.php:
'aliases' => [
// ...
'Form' => Collective\Html\FormFacade::class,
'Html' => Collective\Html\HtmlFacade::class,
// ...
],
Syntax:
Route::get('foo', function () {
return 'Hello World';
});
Example:
Route::group([ 'middleware' => 'auth'], function(){
Route::resource('student', 'StudentController');
});
|-----------------------------------------------------------------------------
| Getting Started with Laravel 5.4
|-----------------------------------------------------------------------------
|
| Laravel 5.4 has more features and Laravel is a free, open-source PHP web
| framework, created by Taylor Otwell and intended for the development of
| web applications following the model–view–controller (MVC) architectural pattern.
|
*/
Database: Migrations
#Generating Migrations----------------------------
To create a migration, use the make:migration Artisan command:
Syntax:
php artisan make:migration create_users_table
The new migration will be placed in your database/migrations directory. Each migration file name contains a timestamp which allows Laravel to determine the order of the migrations.
Example:
vagrant@homestead:~/sample$ php artisan make:migration create_student_table
Created Migration: 2017_08_01_044007_create_student_table
--------------------
#Running Migrations
--------------------
To run all of your outstanding migrations, execute the migrate Artisan command:
Syntax:
php artisan migrate
If you are using the Homestead virtual machine, you should run this command from within your virtual machine.
#Tables
#Creating TablesTo create a new database table, use the create method on the Schema facade. The create method accepts two arguments. The first is the name of the table, while the second is a Closure which receives a Blueprint object that may be used to define the new table:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
});
Of course, when creating the table, you may use any of the schema builder's column methods to define the table's columns.
Eloquent: Getting Started
#Defining Models------------------
To get started, let's create an Eloquent model. Models typically live in the app directory, but you are free to place them anywhere that can be auto-loaded according to your composer.json file. All Eloquent models extend Illuminate\Database\Eloquent\Model class.
The easiest way to create a model instance is using the make:model Artisan command:
Syntax:
php artisan make:model Models/Student
Example:
vagrant@homestead:~/sample$ php artisan make:model Models/Student
Model created successfully.
Controllers
#Resource Controllers----------------------
Laravel resource routing assigns the typical "CRUD" routes to a controller with a single line of code. For example, you may wish to create a controller that handles all HTTP requests for "photos" stored by your application. Using the make:controller Artisan command, we can quickly create such a controller:
Syntax:
php artisan make:controller StudentController --resource
This command will generate a controller at app/Http/Controllers/StudentController.php. The controller will contain a method for each of the available resource operations.
Next, you may register a resourceful route to the controller:
Route::resource('Student', 'StudentController');
This single route declaration creates multiple routes to handle a variety of actions on the resource. The generated controller will already have methods stubbed for each of these actions, including notes informing you of the HTTP verbs and URIs they handle.
Example:
vagrant@homestead:~/sample$ php artisan make:controller StudentController --resource
Controller created successfully.
Validation
#Form Request Validation------------------------
#Creating Form Request
-----------------------
For more complex validation scenarios, you may wish to create a "form request". Form requests are custom request classes that contain validation logic. To create a form request class, use the make:request Artisan CLI command:
Syntax:
php artisan make:request StoreBlogPost
The generated class will be placed in the app/Http/Requests directory. If this directory does not exist, it will be created when you run the make:request command.
Example:
vagrant@homestead:~/student$ php artisan make:request StudentRequest
Request created successfully.
LaravelCollective
#Forms & HTML---------------
#Installation
--------------
Begin by installing this package through Composer. Edit your project's composer.json file to require laravelcollective/html.
Syntax:
composer require "laravelcollective/html":"^5.4.0"
Example :
vagrant@homestead:~/sample$ composer require "laravelcollective/html":"^5.4.0"
fig:Laravel Collective installing via composer |
Next, add your new provider to the providers array of config/app.php:
'providers' => [
// ...
Collective\Html\HtmlServiceProvider::class,
// ...
],
Finally, add two class aliases to the aliases array of config/app.php:
'aliases' => [
// ...
'Form' => Collective\Html\FormFacade::class,
'Html' => Collective\Html\HtmlFacade::class,
// ...
],
Routing
The most basic Laravel routes simply accept a URI and a Closure, providing a very simple and expressive method of defining routes:Syntax:
Route::get('foo', function () {
return 'Hello World';
});
Example:
Route::group([ 'middleware' => 'auth'], function(){
Route::resource('student', 'StudentController');
});
Comments
Post a Comment