Tag: laravel

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 “
directory.

Template Inheritance:

When defining a view you use the Blade @extends directive to extend the blade layout and blade all section using
@section directives.
@extends('layouts.app')
 
@section('title', 'Page sidebar')
 
@section('sidebarDemo')
@parent
 
<p>This is sidebar demo.</p>
@endsection
 
@section('content')
<p>This is body content.</p>
@endsection
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!
@else
    I don't have any records!
@endif    
Loops:-

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 }}
@endfor
 
@foreach ($demos as $demo)
    <p>This is demo{{ $demo->id }}</p>
@endforeach
 
@forelse ($demos as $demo)
    <li>{{ $demo->name }}</li>
@empty
    <p>No users</p>
@endforelse
 
@while (true)
    <p>I'm looping forever.</p>
@endwhile
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.
Advertisements

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
<?php 
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
<?php
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;
      $todo->save();
      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.