Laravel: Difference between revisions

From Network Security Wiki
Content added Content deleted
Line 370: Line 370:
''Seeding: RecordsTableSeeder''
''Seeding: RecordsTableSeeder''


You will be able to see this dummy data in phpmyadmin now,
You will be able to see this dummy data in phpmyadmin now.

Revision as of 09:53, 20 September 2017

Installation

sudo apt install composer

Creating a project named 'code':

composer create-project --prefer-dist laravel/laravel hello
cp .env.example .env
php artisan key:generate
sudo chmod -R 777 storage/

Testing output:

php artisan serve --host 0.0.0.0 --port 8082

Edit hosts file:

sudo nano /etc/hosts
192.168.1.35	hello.dev

Create a new apache conf file:

sudo nano /etc/apache2/sites-available/laravel.conf
<VirtualHost *:80>
    ServerName hello.dev

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/hello/public

    <Directory /var/www/html/hello/public>
        AllowOverride All
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
sudo nano /etc/apache2/ports.conf
Listen 80
sudo a2ensite laravel.conf
sudo service apache2 restart

The site should be available now at:

http://hello.dev/

Auth

Enable Authentication

php artisan make:auth


Site Creation

ROUTES

cd routes/
sudo nano web.php
Route::get('contact', function () {
    return 'Hello from Aman';
});

Route::get('contact/{category}', function ($category) {
    return 'Hello from '.$category. ' Aman';
});

Check resulting pages:

http://192.168.1.35/hello/public/contact/
http://192.168.1.35/hello/public/contact/Test

VIEWS

Create a new View file:

cd ../resources/views/
sudo touch master.blade.php
sudo nano master.blade.php

Paste below contents:

<!DOCTYPE html>
<html lang="en">
<head>

	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE-edge">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>HELLO</title>

</head>
<body>

	<nav class="navbar navbar-inverse">
	<div class="container-fluid">

	 <div class="navbar-header">
		<a class="navbar-brand" href="{{url('/home')}}">Home</a>
	 </div>

	 <div>
		<ul class="nav navbar-nav">
		<li><a href="{{url('/view')}}">View All</a></li>
		<li><a href="{{url('/new')}}">Add New</a></li>
		<li><a href="{{url('/edit')}}">Edit/Delete</a></li>
		</ul>
	 </div>

	</div>
	</nav>
<!-- Online Links -->
	<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
	<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.1/js/bootstrap.min.js"></script>
	<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" >

	<div class="container">
	  @yield('content')
	</div>


</body>
</html>


Edit Routes for this file:

cd ../../routes/
sudo nano web.php 
Route::get('/home', function () {
    return view('home');
});

Route::get('/view', function () {
    return view('view');
});

Route::get('/new', function () {
    return view('new');
});

Route::get('/edit', function () {
    return view('edit');
});

Create above defined VIEW page:

cd ../resources/views/
sudo touch view.blade.php 
sudo nano view.blade.php

Paste below contents:

@extends('master')

@section('content')

VIEW all content goes here

@endsection

Similarly create HOME page:

sudo nano home.blade.php
@extends('master')

@section('content')

HOME all content goes here

@endsection

Similarly create NEW page:

sudo nano new.blade.php
@extends('master')

@section('content')

NEW all content goes here

@endsection

Similarly create EDIT page:

sudo nano edit.blade.php
@extends('master')

@section('content')

EDIT all content goes here

@endsection

Test above page:

http://192.168.1.35/hello/public/home

CONTROLLERS

  • Routes page can call URLs but this is for small applications only.
  • For industrial applications, use Controllers.

Create a Controller:

sudo php artisan make:controller RecordController

Edit the above generated file:

cd app/Http/Controllers/
sudo nano RecordController.php 

Append below contents:

class RecordController extends Controller
{
  public function showAll()
        {
                return view('view');
        }

  public function showNew()
        {
                return view('new');
        }

  public function showEdit()
        {
                return view('edit');
        }
}

Update the Routes file to use above controllers:

cd ../../routes/
sudo nano web.php

Append below content:

Route::get('/view', 'RecordController@showAll');

Route::get('/new', 'RecordController@showNew');

Route::get('/edit', 'RecordController@showEdit');

DATABASE

  • Easy to create & manage tables with Migrations in large environments.

Create a new DB 'hello' using phpmyadmin or below SQL Query:

create database hello;
grant all privileges on hello.* to aman@localhost identified by 'pwd@123';

Update the ENV file with this information:

sudo nano .env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=hello
DB_USERNAME=aman
DB_PASSWORD=pwd@123


Create a DB table using Artisan:

sudo php artisan make:migration create_records_table
Created Migration: 2017_09_19_190352_create_records_table

File is created under below location:

cd database/migrations/ && ls
2017_09_19_190352_create_records_table.php

Edit this file with below DB Table information:

sudo nano 2017_09_19_190352_create_records_table.php
    public function up()
    {
        Schema::create('records', function (Blueprint $table) {
            $table->increments('id');
            $table->string('country');
            $table->string('city');
            $table->string('date');
            $table->string('no_of_aliens');
            $table->string('color_of_aliens');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('records');
    }

Create the Tables:

sudo php artisan migrate

You can confirm the Tables created in phpmyadmin.

ELOQUENT

Create a model named Record

sudo php artisan make:model Record 
cd app/
sudo nano Record.php

Add below information

class Record extends Model
{
    protected $table='records';
    protected $fillable=['id','country','city','date','no_of_aliens','color_of_aliens'];

}

To Feed test data using Seeding, create seed file:

sudo php artisan make:seeder RecordsTableSeeder
cd database/seeds/
sudo nano RecordsTableSeeder.php 

Append below information:

use App\Record;    //to import records.php file

class RecordsTableSeeder extends Seeder
{
  public function run()
    {
       for ($i=0; $i<20; $i++)
        {
           Record::create
            (
                [
                   'country' => "country$i",
                   'city' => "city$i",
                   'date' => "date$i",
                   'no_of_aliens' => "no_of_aliens$i",
                   'color_of_aliens' => "color_of_aliens$i"
                ]
            );
        }
    }
}

Edit the Databse seeder file:

sudo nano DatabaseSeeder.php 
use App\Record;

class DatabaseSeeder extends Seeder
{
    public function run()
    {
        $this->call('RecordsTableSeeder');
    }

}

Now seed the file with dummy data:

sudo php artisan db:seed
Seeding: RecordsTableSeeder

You will be able to see this dummy data in phpmyadmin now.