Understand Basic Of Laravel Contracts

Understand Basic Of Laravel Contracts


  • Creator of Laravel, Taylor Otwell brings awesome features in Laravel 5 called Contracts.
  • Contracts are a set of interfaces which helps a developer to make code loosely coupled base OR in another term we can say Contracts are interfaces that provide core services.
  • So in common, Laravel Contracts is a wrapper of different interfaces that property of `illuminate/contracts`.
Now what are the ‘Facades’?:
Facades also interface and like a Contracts so let’s first understand what are the Facades.
  • Do they have any similarity or relationship or anything else? Let’s understand something about Facades first.
  • Facades are also static interfaces that provide access methods to the classes of service container.
  • We can use Facades `type­hint` like we already implement the route.
Route::bind('mystore', function ($id){
 return App\Store::get();
In the above code, Facades solved the contracts out of service container. But the quality of Contracts is we can define conspicuous dependencies for your classes and make your application more loosely coupled. Here Facades work fine but in some case, we want to create something extra, you need to use Contracts.
How to write?
Contracts implementation is very simple. Let’s understand the Contracts implementation with sample example:
Suppose I have a ‘StoreController’ with that I will maintain my all store list and for that, I need insert these data into the database. So what I will do, I’ll create an eloquent model ‘Store’ and bind it with a route into route.php.
After that, I will use that model ‘Store’ all CRUD operations using the resource. In between, first I check whether the user is logged in OR not? If logged in then only can able act on model CRUD otherwise not. Example code:
public function insert(Request $request)
  if (Auth::check()) {
     // If user is logged in...
From the above code, I used Facades `Auth` to check the user is logged in OR not? So here if I don’t want more decoupled state it works fine and it’s what? code with SOLID principle and here Contracts comes for the solution. For that what I do? just inject ‘Auth’ Facade through constructor:
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard as Auth;

class StoreController extends Controller
     * Display a listing of the resource.
     * @return Response
    protected $auth;
    public function __construct(Auth $auth) {//Inject `Auth` Facade...
     $this­->auth = $auth;    

    public function insert(Request $request)
As you see, when we use this line of code in our ‘StoreController’: use Illuminate\Auth\Guard as Auth;`
We actually inject an instance based on concretion, not an abstraction. Whenever we call any methods in this instance it is aware of your framework. But we need to make it completely unaware of our Framework and become loosely coupled.
To do that we just have to change one line of code in our ‘StoreController’ instead of that line of code just change `use Illuminate\Auth\Guard as Auth;` to `use Illuminate\Contracts\Auth\Guard as Auth;` and do not need to make change the store() method. This is what loosely coupled provided by Contracts.
When to use?
  • In some situation, many developers have use facades and others have use contracts and its depends on developers logic and them test and like as developer teams. contracts and facades both are used to create Laravel applications.You will look few practical differences when your class keeping responsibilities.
  • In some case, code requirement is changed to use some specific code/package/service what we do is? we need to change the controller code. but this is the correct way? No. because this will consume our time to re-code that. But is we use that contract and implement that method like service which adheres to the contract and implements those methods. Let’s understand with an example to more clarity:
I have created a `ShippingServiceContract` with methods like `getShippingRegions()`, `getShippingCost()`, `getShippingTrackStatus()`…etc like all basic methods.And create `BludartDeliveryService` which again adhere to Contract and implements those methods.
Now I will use this `ShippingServiceContract` Contract by injecting it into my controller which is called dependency injection and start using it. After a few days, I think, I don’t want to use Bludart and for replacement, I need to use some other shipping provider. So now what I need to do is, just create `SomeOtherDeliveryService` that also adhere to Contract and change binding from `SomeOtherDeliveryService` to `BludartDeliveryService` and no need to change the controller code that’s it.
For the binding between contract and implementation, we can use the service provider such as `app/Providers/AppServiceProvider.php`
The Laravel Contracts package provides a special way to extend Laravel core components explicitly and provides a stable API for developers to access the framework’s behaviour.

Hope this blog is quickly and easily guide for Laravel Contracts. Need more assistance regarding Laravel Web Development Services Contact us now!


Understand Laravel Eloquent Relationships

ND- May

Laravel relationship tables are often connected to one another in the database.
Laravel support different types of relationships and it’s easily working and managing data.
Hire Laravel Developer because Laravel provides powerful query builder for the defined relationship for Database tables and relationship Eloquent model provides capabilities for querying and powerful chaining method.

Defining Relationships:-

  • One to One
  • One to Many
  • Many to Many
  • Has Many Through
  • Polymorphic Relations
  • Many To Many Polymorphic Relations

One to One:-

One to One is a basic relation in the relationship of database tables. In this relationship hasOne() method call and return result value :

Understand Laravel Eloquent Relationships
  • In hasOne method first argument passed is the name of the related model. Once a relationship is defined, we use Eloquent dynamic properties for retrieve related records.
  • Relationship model automatically assumes foreign key to id. if we wish to override or pass the third argument to specify your custom key.

    return $this->hasOne(‘App\Demo, ‘foreign_key’, ‘local_key’);

  • Now, we can access Demo model to our Test model. We can define belongsTo() method in Test model for access relationship Demo model.
Understand Laravel Eloquent Relationships

One to Many:-

One to Many relationships is defined in a single model to use relationships other models data.

Understand Laravel Eloquent Relationships
  • This relationship defined to hasMany() method.
  • In this relationship, we can create a relationship in a single model same as One to One inverse model. After defining relationship we can retrieve demo model for the exam.

Many to Many:-

  • Many to Many relations are a little complex to hasOne and hasMany relationships.
  • This relationship is defined by ‘belongsToMany’ method.
Understand Laravel Eloquent Relationships
  • After defining relationship you can access student’s exams using dynamic property exam.
  • You can use chaining query constraints on this relationship.
  • You can simply call belongsToMany() on your related model when inverse of Many to Many relationships.
Understand Laravel Eloquent Relationships
  • In this relationship you can see both models counterpart are same because we are reusing the belongsToMany() on this models

Has Many Through:

  • Many convenient shortcuts are used to access distance relations in this relationship and all convenient shortcuts are provided by Has Many Through relationship.
  • Now, we can define Has Many Through relationship in Demo model.
Understand Laravel Eloquent Relationships
  • In above example first argument and second argument pass in hasManyThrough() method is the name of the final method and intermediate model name.

Polymorphic Relations:

  • Single associated model is allowed to belong more than one another model in Polymorphic relationship.
  • This relation provides a custom Polymorphic relation
Understand Laravel Eloquent Relationships
  • You can register morphMap() in boot function in AppServiceProvider or Create a separate service.
  • The model structure is Build a relationship in Polymorphic relationship.
Understand Laravel Eloquent Relationships
  • The polymorphic relation is a great deal to save time and share particular features.

Many To Many Polymorphic Relations:

  • Many To Many Polymorphic Relation Is defined in traditional polymorphic relations. For example, teacher and exam model is shared a polymorphic relation with a demo model.
  • It can allow you to unique tags in a single list and that shared in teacher and exam models.
  • Model structure of this relationship is based on Eloquent class.
Understand Laravel Eloquent Relationships
  • After that inverse of the relationship of exams model. So you can define its related models and define teacher and test method in below example :
Understand Laravel Eloquent Relationships

All You Need To Know About Blade Templates In Laravel

Blade Templates In Laravel
Let’s start with the introduction of Blade and how Laravel supports it strongly. Laravel provides a simple and powerful templating engine like as blade. You can also use PHP code in blade template views.
In fact, all blade views are compiled into plain PHP code. Blade view files use the “.blade.php “  extension and it stored in “ resources/views “

Template Inheritance:

When defining a view you use the Blade @extends directive to extend the blade layout and blade all section using
@section directives.
@section('title', 'Page sidebar')
<p>This is sidebar demo.</p>
<p>This is body content.</p>
Here, we are extending layouts.app blade view so we use all section @section directives @parent directive is an append
directive to the layout’s sidebar.
Blade view may be returned from routes.
Route::get(blade/name, function () {
    return view('blade.name');
Displaying Data:-
You may Display data in blade bypass the variable in curly braces like:
 Route::get('bladeDemo', function () {
        return view('welcome', ['name' => 'Addweb']);
Here, we add the ‘Data’ in name variable so, we display the name like,
 Hello {{ $name }}.
It has not limited to displaying content, in fact, you can also put any PHP code and result of any PHP functions inside of blade echo statement.
{{ isset($name) ? $name : Test }}
Here, we use the ternary operator to check the name is exist or not. if a name exists and set so it displays name and not set name so it can display Default.

Control Structures:-

The blade provides the displaying data but in additionally blade also provides the convenient shortcut of PHP control

structure such as conditional statement and loops.
If statement:-
You may use the @if, @elseif, @else, @endif directives for the if construct.These directives function identically to their PHP counterparts.
@if (count($records) === 1)
    I have one record!
@elseif (count($records) > 1)
    I have multiple records!
    I don't have any records!

Blade provides simple directive parts for a looping and it is identical to their PHP counterparts

@for ($i = 0; $i < 10; $i++)
        The current value is {{ $i }}
@foreach ($demos as $demo)
    <p>This is demo{{ $demo->id }}</p>
@forelse ($demos as $demo)
    <li>{{ $demo->name }}</li>
    <p>No users</p>
@while (true)
    <p>I'm looping forever.</p>
The Loop variables:-

When a loop is working a $loop is available inside of a loop. This variable provides some variable to use bits of information such as the current loop of an index and first or last iteration of a loop.

Hope this helped you to get most out of the system. Feel free to share your reviews or need assistance for Hire Laravel Developer then get in touch with us. Pick the best answer for your requirements.


AdminLTE Implement Skills In Laravel 5
Let me walk you through AdminLTE Introduction:

  • AdminLTE is a popular open source WebApp template for admin Dashboard and control panel.It is responsive template using Bootstrap 3 and CSS framework.
  • AdminLTE is now used by thousands of users around the world.
  • AdminLTE is a based on modular design and it easily customizes and built upon.
  • This package is switched default laravel scaffolding / boilerplate to adminLTE template.
  • In laravel AdminLTE is managed by “ Bower “.

Feature of AdminLTE:

  • AdminLTE provides Sidebar mini feature to the user with the ability to collapse the sidebar without losing accessibility to side navigation.
  • Control sidebar is a new section of the layout. This sidebar use for many purposes in right sidebar
    • AdminLTE provides the 6 new skin originally. That switch the color of sidebar in dashboard.

Integrate AdminLTE:

  • If you have integrated AdminLTE so first you have to create a new project using laravel because it is easy to use and more comfortable.
  • First, You have to use laravel installer, In the terminal type the command.

composer global require “laravel/installer=~1.1”

  • Here, this command will install composer in few minutes. Then get the fresh copy of laravel using below command.

laravel new myapp

  • Here,  we create new laravel copy and its name is myapp.
  • Laravel setup is completely installed in your system then you have able to see the welcome page on your site.
  • Once, it’s process is done then after you have navigated to the public directory in a terminal command.
  • After you have navigated to the public directory you have to install AdminLTE using Bower

    bower install admin-lte

  •  Once, you have to successfully installed adminLTE in laravel then you should have folder called bower_componets and inside it you will find admin-lte.
  • Laravel is provide the greate templating system out of the box called Blade. Then after we have to convert the regular HTML page to Blade.
  • First you have to create a new blade in “ resources/view “ directory and then you have to create route for this blade.
  • If you have to create the route and blade then you will copy the “ bower_components/admin-lte/starter.html ” in new view and modify the code. Then after you will visit the page so it has blank.
  • Then after you will split the template into the three files like sidebar.blade.php, header.blade.php, footer.blade.php . so you have create new blade in view directory.
  • Then after you have create a new controller using the controller command

    php artisan make:controller TestController –plain

  • After creating controller you will do work in controller and you will create the new route for this controller

    Route::get(‘test’, ‘TestController@index‘);

  • After creating route you will check your test page so you will display the output for your developing view.

Install AdminLTE template:

  • A laravel 5 package that switch the default  Laravel scaffolding / boilerplate to adminLTE template with bootstrap 3.0.
  • AdminLTE is the so simple to install and use.
  • AdminLTE is install globally with compose

       composer global require “acacha/adminlte-laravel-installer=~2.0”

  • And convert any fresh laravel installation to AdminLTE to use with:-

    laravel new laravel-with-admin-lte cd laravel-with-admin-lte adminlte-laravel install

  • It successfully installed so you can use AdminLTE if you are use llum.
  • You are start to using AdminLTE project. Llum will be configure database (sqlite), execute migrations , install devtools and serve for you to use it.

Hope this helped you to get most out of the system. Feel free to share your reviews and need assistance for Hire Laravel Developer.

Understand The Implementation of REST API In Laravel 5

In this blog, we are going to demonstrate the understanding to implement REST API in Laravel 5. Starting with some quick introduction part;
  • Rest API is backend implementing also it is a simple basic HTTP authentication to store data for different users of the application.
  • We will use the middleware to provide the authentication for the rest call.
  • The middleware will authorize the user at every request without storing in session or using token.
Implement REST API:
composer create-project restAPP –prefer-dist
  • This terminal command to we create the new application and that name is restAPP.
  • Then after we create the model for basic authentication and we are using the email for login parameter.
  • Complete the project creation after we have created the controller and model, using artisan command.
    php artisan make:model Todo
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\User;
class Todo extends Model {
 protected $fillable = ['title','isDone'];
    public function user(){
        return $this->belongsTo('App\User');
In the model we have to add the reference to the user model and method to retrieve it.
    php artisan make:controller TodoController
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Todo;
use Auth;
use Request;
class TodoController extends Controller {
    * Display a listing of the resource.
    * @return Response
   public function index() {
         $todos = Todo::where('user_id','=',Auth::user()->id)->get();
     return $todos;
    * Store a newly created resource in storage.
    * @return Response
   public function store() {
      $todo = new Todo(Request::all());
      $todo->user_id = Auth::user()->id;
      return $todo;
In controller, we have to add a basic method to create and retrieve data from the logged users.
It’s all complete then we have to create a middleware to for authentication for possible to access data only if the user passes in the HTTP request his email and password. Let’s create middleware.
    php artisan make:middleware simpleAuthMiddleware
<?php namespace App\Http\Middleware;
use Closure;
class SimpleAuthMiddleware
    * Handle an incoming request.
    * @param  \Illuminate\Http\Request $request
    * @param  \Closure $next
    * @return mixed
   public function handle($request, Closure $next)
      return Auth::onceBasic('username') ?: $next($request);
After creating middleware we have to register in “ kernel.php “ file and give the key for easily use this middleware.
‘simpleauth’ => ‘App\Http\Middleware\SimpleAuthMiddleware’, After register this middleware we have to create the route path for Todo Controller and middleware
Route::get('api/todo', ['uses' => 'TodoController@index','middleware'=>'simpleauth']);
Route::post('api/todo', ['uses' => 'TodoController@store','middleware'=>'simpleauth']);
If you try with the postman on every request of email and password will be prompted. You will also pass this info on URL or HTTP header.

Hope this helped you to get most out of the system. Feel free to share your reviews or need assistance for Hire Laravel Developer then get in touch with us. Pick the best answer for your requirements.