Wednesday, 26 April 2017

How to create helper in laravel?

Basically there are there steps.

Step 1:    Within your app/Libraries directory, create a Helpers.php file and add your functions.
Step 2:    Within composer.json, in the autoload block, add "files": ["app/Libraries/helpers.php"].
Step 3:    Run composer update.
Step 4:    Showing the content. 
Helpers.php
<?php
    function notification_count()
    {
        $user_loggedin = Auth::user();
        $id = $user_loggedin->id;  
        $sql =  DB::table('notification')
                ->where('read_status', 0)
                ->where('user_id',$id)
                ->count('read_status');
    echo $sql;                            
    } 
?>

composer.json

"files": [
        "app/Libraries/Helpers.php"
        ],   
       
Showing the content
 <span class="badge" style="color:#FFFFFF !important;">{{ notification_count() }}</span> <i class="fa fa-caret-down"></i></a>

CRUD

Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use DB;
use Session;

class Flight extends Model {
    protected $table = 'flight';
    protected $primaryKey = 'id';
    // protected $guarded = [];
    // protected $hidden = ['id'];
    protected $fillable = ['name','age','status'];
    public $timestamps = true;


    public function updateflight ($id,$data)
    {
      $updatedRows = App\Flight::where('id', $id)->update('name'=>$data['name'],'status'=>$data['status'])->save();
      return  $updatedRows;   
    } 

    public function deleteflight ($id)
    {
      $deletedRows = App\Flight::where('id', $id)->where('status','0')->delete();
      return  $deletedRows;   
    }
}


?>
Controller

<?php
namespace App\Http\Controllers;
use App\Models\Flight;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Session;
use DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;

class FlightController extends Controller
{  
    public function addrecord(Request $request)
    {
    
       $postData = Input::all();
     
        $messages = array(
             'name.required'=>'You cant leave name field empty',
             'age.required'=>'You cant leave age field empty',
             'status.required'=>'Age cannot be blank',
    );

    $rules = array(
        'name'=>'required | min:5',',
        'age'=>''required ',
        'status'=>'required',
    );
   
     $validator = Validator::make($postData, $rules,$messsages);   
     
  if($validator->fails()) {                         
                 return Redirect::to('sales/createnew')->withInput()->withErrors($validator);           
     }else {
       
      $flight = new Flight;
      $flight->name = $request->name;
      $flight->age = $request->age;
      $flight->status = $request->status;
     
      $flight->save();
      return redirect('dashboard')->with('status', 'Sucessfully inserted!');  
      }
}
   
    public function updaterecord(Request $request)
    {
      // Validate the request...
      $id = $request->id;
      $flight = new Flight;
     
      $flight_array = Flight::find( $id );

    //CHECK FOR NEW OR EXISTING Flight
        if( $flight_array ) {
            $flight_array->name = $request->name;
            $flight_array->age = $request->age;
            $flight_array->status = $request->status;
            $flight_array->save();
        }   
    or
   
        $data['name'] = $request->name;
        $data['age'] = $request->age;     
        $data['status'] = $request->status;
   
        $updated = $flight->updateflight($id,$data);
        if($updated  > 0 )
        {          
           Session::flash('success',"Sucessfully updated!");
           return redirect('dashboard');  
           or
           return redirect('dashboard')->with('success', 'Sucessfully updated!');  
        } else {
       
           return redirect('dashboard')->with('error', 'Try again!');  
        }       
       
     }
       
    public function deleterecord(Request $request)
    {
      // Validate the request...
      $id = $request->id;
      $flight = new Flight;     
      $flight_array = Flight::find( $id );

    //CHECK FOR NEW OR EXISTING Flight
    if( $flight_array )
      {
        $flight_array->delete();
      }
   
    Or
        $deletedRows = $flight->deleteflight($id);
        if($deletedRows  > 0 )
        {          
           return redirect('dashboard')->with('status', 'Sucessfully deleted!');  
        } else {
           return redirect('dashboard')->with('error', 'Try again!');  
        }
    }
       
}

?>

**************************************
View file
<form action="" method="post" name="form1">
  <table width="200" border="1">
    <tr>
      <td colspan="2">
@if(Session::has('success'))
<div class="alert alert-success">{{ Session::get('success') }}</div>
@endif

Error on view page for all the errors.
@foreach ($errors->all() as $error)
   <p class="alert alert-danger">{{ $error }}</p>
@endforeach

</td>
     
    </tr>
    <tr>
      <td>Name</td>
      <td>

     <input type="text" class="form-control" id="name" placeholder="Name" name="name" value="{{ old('name') }}">
        <?php if($errors->first('name') ){ ?><div class="alert alert-danger"> {{ $errors->first(name') }}</div><?php }  ?>

      </td>
    </tr>
    <tr>
      <td>Age</td>
      <td>
  <input type="text" class="form-control" id="age" placeholder="Age" name="age" value="{{ old('age') }}">
       <?php if($errors->first('age') ){ ?><div class="alert alert-danger"> {{ $errors->first(age') }}</div><?php }  ?>

      </td>
    </tr>
    <tr>
      <td>Status</td>
      <td><input name="status" type="radio" value="Active">
        <input name="status" type="radio" value="Inactive">

        </td>
    </tr>
  </table>
</form>

Monday, 24 April 2017

How to create active menu selection?


<div class="collapse navbar-collapse">
      <ul class="nav navbar-nav navbar-right">
        <li class="{{ (Request::is('index') ? 'active' : '') }}"><a href="index">Home</a></li>
        <li class="{{ Request::is('about') ? 'active' : '' }}"><a href="about-us">About Us</a></li>
        <li class="{{ Request::is( 'services') ? 'active' : '' }}"><a href="services">Services</a></li>
        <li class="{{ Request::is( 'portfolio') ? 'active' : '' }}"><a href="portfolio">Portfolio</a></li>
        <li class="dropdown {{ Request::is( 'privacy-policy') ? 'active' : '' | Request::is( 'terms-of-use') ? 'active' : '' }}"> <a href="javascript::" class="dropdown-toggle" data-toggle="dropdown">Pages <i class="icon-angle-down"></i></a>
          <ul class="dropdown-menu">
            <li><a href="#">Dropdown Menu 1</a></li>
            <li class="divider"></li>
            <li><a href="privacy-policy">Privacy Policy</a></li>
            <li><a href="terms-of-use">Terms of Use</a></li>
          </ul>
        </li>
        <li><a href="blog">Blog</a></li>
        <li class="{{ Request::is( 'contact-us') ? 'active' : '' }}"><a href="contact-us">Contact</a></li>
      </ul>
    </div>

How to comment in laravel?



It is very necessary to comment in laravel format, not in html or php.

Syntax: {{--    --}}

It will not shown the comment in our page if you see the source code.

Machine Learning - Potato Leaf Disease Prediction

Step 1: import numpy as np import pandas as pd import splitfolders import matplotlib.pyplot as plt import tensorflow as tf from tensorflow i...