Model (Eloquent ORM)
The Eloquent ORM is responsible for the database operations(Model). By using this we can avoid the sql quering. The eloquent model files are placed in app/model directory. Every databse table have a eloquent file.

Here this eloquent will deal with the ‘my_users’ table in database. If we do not specify the table here then laravel will take the plural form of the class name as the table name ie, here it is ‘users’.

We can access a model function by calling in the following manner:
Display corresponding model values: $model = User::display()
Insert value to database:
$user = new User;
$user->name = ‘Test Name’;
$user->save();

Insert value in database using ‘create’ method:
$user = User::create(array(‘name’ => ‘Test Name’)); // Create a new user in the database.
$user = User::firstOrCreate(array(‘name’ => ‘Test Name’)); // Retrieve the user by the attributes, or create it if it doesn’t exist.
$user = User::firstOrNew(array(‘name’ => ‘Test Name’)); // Retrieve the user by the attributes, or instantiate a new instance…

Updating a database values:
$user = User::find(1); //load record by id
$user->email = ‘[email protected]’; //load new value
$user->save(); //update new value

Delete record:
$user = User::find(1); //load record by id
$user->delete(); //delete record

Controllers
Controllers are situated in app/controllers. They are used to perform different actions such as add, edit etc. Controllers are named as “filenameController.php”. The word controller suffixed to each controller file name.

Routers
Router file is presented in app/ directory. Specify urls we are going to create in this file. Example below:

means, when we use url “domain/projectfolder/public/hello” the hello.php file in view folder loaded.

Different types of routers are:
1) GET Route : (if methos is GET)

2) POST Route : (if method is POST)

3)Rout for any HTTP verb:

4)We can add filter in Route:

5)We can add filter to controller action router:

6)We can pass parameters in routes:

To point to a controller in routers:

ie, it will point to a file named UserController and pointed to showProfile function.

Views
The view folder is located in app/ directory. Here all the template files are present. Here the blade template files, other html, PHP files related to designs is present. By using routers we can load these view files.

Templating in laravel
Laravel uses balde template. It is a powerful templating engine for laravel. The blade template files are saved as “filename.blade.php”. The main advantage of template engines are they reduce the amount of code that we write in the view.
We can completely avoid or use html inside this.

For example the form opening and closing in blade template is below:

Double bracket is used to display the output. Eg: {{ $name }} prints the value in variable ‘name’.
No need to specify semicolon. PHP variables can be directly used like above.
‘@’ is used infront of control structures like if, else, for, foreach etc.
for eg:
@if (count($records) === 1)
//your code here
@elseif (count($records) > 1)
//your code here
@else
//your code here
@endif

@{{ This will not be processed by Blade }}: display the content as is in the bracket
@include(‘view.name’): we can include sub views
{{– This comment will not be in the rendered HTML –}}: this is commenting system in balde templating.

Validation using laravel
Laravel support built-in validation. The validation rules are specified in array like below:

$validator->fails() : used to check the validation failed status.

Few basic validation rules and usage are given below:

  • accepted : The field under validation must beyes,on, or1. This is useful for validating “Terms of Service” acceptance.
  • active_url :The field under validation must be a valid URL according to the checkdnsrr PHP function.
  • after:date :The field under validation must be a value after a given date. The dates will be passed into the PHP strtotime function.
  • alpha :The field under validation must be entirely alphabetic characters.
  • alpha_dash :The field under validation may have alpha-numeric characters, as well as dashes and underscores.
  • alpha_num :The field under validation must be entirely alpha-numeric characters.
  • array :The field under validation must be of type array.
  • before:date :The field under validation must be a value preceding the given date. The dates will be passed into the PHPstrtotime function.
  • between:min,max :The field under validation must have a size between the given min and max. Strings, numerics, and files are evaluated in the same fashion as the size rule.
  • confirmed :The field under validation must have a matching field of foo_confirmation. For example, if the field under validation is password, a matching password_confirmation field must be present in the input.
  • date :The field under validation must be a valid date according to the strtotime PHP function.
  • date_format:format :The field under validation must match the format defined according to the date_parse_from_format PHP function.
  • different:field :The given field must be different than the field under validation.
  • digits:value :The field under validation must be numeric and must have an exact length of value.
  • digits_between:min,max :The field under validation must have a length between the given min and max.
  • email :The field under validation must be formatted as an e-mail address.
  • exists:table,column :The field under validation must exist on a given database table.

Retreving inputs
we can access form input values by the following code:
Input::all() : to get all input values.
Input::only(‘username’, ‘password’) : here only select these two field values.
Cookie::get(‘name’) : to get cookie value named ‘name’.
Input::file(‘photo’) : to retrieve file uploaded.
Input::file(‘photo’)->move($destinationPath, $fileName) : to move file to directory.
Input::flashOnly(‘username’, ’email’) : flashing input value to session for next request.
Input::flashExcept(‘password’) : all other values except password will be stored in session for next request.
Input::old(‘username’) : get the old value.

Sessions
Session::put(‘key’, ‘value’) :storing session value.
$value = Session::get(‘key’) : get session value.
$data = Session::all() : get all session datas.
Session::flush() :remove all session values.

Let’s watch beauty of laravel!