Tag Archive Laravel 5

laravel ajax method delete

เขียน ui laravel 5.1 อยู่ แบบใช้ ajax update ข้อมูล โดยเดิมมันใช้ form ธรรมดาแต่ใช้ method เป็น DELETE ทดลองส่งคำสั่งผ่าน ajax อยู่นานก็ไม่ได้ผล แถมมันวิ่งไปที่อื่นอีกตะหาก ทดลองเขียน php ธรรมดามาลองดูก็ไม่เจออะไร

นั่งงงอยู่นาน ก็ pass ค่าไปถูก เขียน php ธรรมดาไป ก็ได้ค่าไปถูก จน search ไปเจอ Delete request Jquery Ajax doesn’t work ที่แท้ laravel ถ้าจะใช้ method delete ต้องใช้ “method”: “POST” กับตัวแปร “_method”: “delete”[code language=”javascript”]
$(‘.glyphicon-trash’).click(function(e) {
e.preventDefault();

var r = confirm(‘Are you sure?’);
if(r == true) {
$.ajax({
"data": {
"_method":"DELETE",
"_token": "{{ csrf_token() }}",
},
"success": function(result) {
location.reload();
alert(‘success’);
},
"type": "POST",
"url": "/admin/index",
});
}
});
[/code]ใช้ได้แล้ว แต่น่าจะเขียนเป็นคู่มือเอาไว้นะ

CRUD ใน laravel 5

บอกเลย CRUD (Create, read, update and delete generator) ใน laravel 5 หายากมากจน GII ของ YII ดูเทพขึ้นมาทันที ตัวที่พอจะเทียบได้ก็มี Laravel Generator

ติดตั้งโดย

  1. เพิ่ม package InfyOmLabs/laravel-generator ในไฟล์ composer.json[code language=”text” title=”composer.json”]
    "require": {

    "infyomlabs/adminlte-templates": "dev-master",
    "infyomlabs/generator-builder": "dev-master",
    "infyomlabs/laravel-generator": "dev-master",
    "infyomlabs/swagger-generator": "dev-master",
    "jlapp/swaggervel": "dev-master"
    "laravelcollective/html": "5.2.*"

    },
    [/code]
  2. run command[code language=”text” title=”command”]composer update[/code]
  3. เปิดไฟล์ \config\app.php เพิ่ม[code language=”php” title=”\config\app.php”]

    ‘providers’ => [

    \InfyOm\Generator\InfyOmGeneratorServiceProvider::class,
    \InfyOm\GeneratorBuilder\GeneratorBuilderServiceProvider::class,
    Collective\Html\HtmlServiceProvider::class,
    Laracasts\Flash\FlashServiceProvider::class,
    Prettus\Repository\Providers\RepositoryServiceProvider::class,

    ],

    ‘aliases’ => [

    ‘Flash’ => Laracasts\Flash\Flash::class,
    ‘Form’ => Collective\Html\FormFacade::class,
    ‘Html’ => Collective\Html\HtmlFacade::class,

    ],[/code]

  4. Publish ไฟล์ไปโฟลเดอร์ public โดย run command[code language=”text” title=”command”]php artisan vendor:publish
    php artisan infyom:publish[/code]
  5. Run routes Publish Command[code language=”text” title=”command”]php artisan infyom.publish:generator-builder[/code] จะเพิ่ม rute [code language=”php”]Route::get(‘generator_builder’, ‘\InfyOm\GeneratorBuilder\Controllers\[email protected]’);
    Route::get(‘field_template’, ‘\InfyOm\GeneratorBuilder\Controllers\[email protected]’);
    Route::post(‘generator_builder/generate’, ‘\InfyOm\GeneratorBuilder\Controllers\[email protected]’);[/code]
  6. Publish view โดย run command[code language=”text” title=”command”]php artisan infyom.publish:generator-builder –views[/code]
  7. เปิดไฟล์ \config\infyom\generator_builder.php แก้เป็น[code language=”php” title=”\config\infyom\generator_builder.php”]<?php

    return [

    ‘views’ => [

    ‘builder’ => ‘infyom.generator-builder.builder’,

    ‘field-template’ => ‘infyom.generator-builder.field-template’
    ]
    ];[/code]

  8. เปิดไฟล์ \config\infyom\laravel_generator.php แก้ ‘templates’ => ‘core-templates’, เป็น ‘templates’ => ‘adminlte-templates’,

ทดลองใช้

  1. ทดลองเรียกดู http://localhost/…/generator_builder
  2. แต่กรอกอะไรก็เจอ error Fail!result ลองจับ traffic ดูก็เห็นว่ามี error MethodNotAllowedHttpException in RouteCollection.php line 218: เปิดไฟล์ \resources\views\infyom\generator-builde\builder.blade.php บรรทัด 321 แก้ type: “POST” เป็น method: “POST”,

สร้าง backend ใน laravel 5

ข้ามไปอ่าน Laravel: AdminLTE จะอัพเดตกว่า

ผมเลือก AdminLTE มาใช้เป็น backend เพราะว่าเขียนโดยใช้ bootstrapt สวยพอประมาณ มีตัวอย่างเยอะ และที่สำคัญแจกฟรี และคุณ Sergi Tur Badenas แปลงให้ใช้กับ laravel 5 ไว้ให้เรียบร้อยแล้ว (framework อื่นก็มีครับ ตาม link ผมก็ใช้ใน YII2 อยู่เหมือนกัน)

ติดตั้งโดย

  1. เพิ่ม package acacha/adminlte-laravel ในไฟล์ composer.json[code language=”text” title=”composer.json”]
    "require": {

    "acacha/admin-lte-template-laravel": "2.*"

    },[/code]
  2. run command[code language=”text” title=”command”]composer update[/code]
  3. เปิดไฟล์ \config\app.php เพิ่ม[code language=”php” title=”\config\app.php”]

    ‘providers’ => [

    Acacha\AdminLTETemplateLaravel\Providers\AdminLTETemplateServiceProvider::class,
    App\Providers\RouteServiceProvider::class

    ],

    ‘aliases’ => [

    ‘AdminLTE’ => Acacha\AdminLTETemplateLaravel\Facades\AdminLTE::class,

    ],[/code]

  4. Publish ไฟล์ไปโฟลเดอร์ public โดย run command[code language=”text” title=”command”]php artisan vendor:publish –tag=adminlte –force[/code]

สร้าง backend

  1. สร้างไฟล์ \app\Http\Controllers\AdminController.php[code language=”php” title=”\app\Http\Controllers\AdminController.php”]<?php

    namespace App\Http\Controllers;

    use App\User;
    use App\Http\Controllers\Controller;

    class AdminController extends Controller {

    /**
    * Show the profile for the given user.
    */
    public function index()
    {
    return view(‘home’);
    }

    }
    [/code]

  2. เพิ่ม route ในไฟล์ \app\Http\routes.php[code language=”php” title=”\app\Http\routes.php”]

    Route::get(‘admin/’, ‘[email protected]’);

    [/code]

สร้าง muti site ใน laravel

ต้องการสร้างเว็บรองรับหลายๆ บริษัท โดยที่ใช้ code ชุดเดียวกันและให้ฝ่ายที่ดูแลสามารถเพิ่มข้อมูลและเว็บไซต์ ได้เองโดยที่ไม่ต้องใช้ความรู้ทางด้านการเขียนโปรแกรมเลย

เจอ package ชื่อ delatbabel/site-config แต่เพราะว่าเค้ารองรับแค่ laravel 5.1 เลยต้องสร้าง project ใหม่ตั้งแต่ต้นเลย

  1. พิมพ์ command[code language=”text” title=”installation 1=5.1″]composer create-project laravel/laravel sites "5.1.*"[/code]
  2. แก้ config ในไฟล์ .env ให้ติดต่อ databse ได้[code language=”text” title=”.env”]…
    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_DATABASE=sites
    DB_USERNAME=root
    DB_PASSWORD=xxx
    …[/code]
  3. เพิ่ม package ในไฟล์ composer.json[code language=”php” title=”composer.json”]…
    "require": {
    "php": ">=5.5.9",
    "laravel/framework": "5.1.*",

    "delatbabel/site-config": "~1.0"
    },
    …[/code]
  4. run command[code language=”text” title=”command”]
    composer update
    [/code]
  5. เปิดไฟล์ config\app.php เพิ่ม[code language=”php” title=”configpp.php”]

    ‘providers’ => [

    Delatbabel\SiteConfig\SiteConfigServiceProvider::class,

    ],

    ‘aliases’ => [

    ‘SiteConfigSaver’ => Delatbabel\SiteConfig\Facades\SiteConfigSaver::class,

    ],
    …[/code]

  6. เพิ่ม Boostrap the Config Loader ใน 2 ไฟล์ คือ \app\Console\Kernel.php และ \app\Http\Kernel.php ต้องครบ 2 ไฟล์นะครับ ผมใส่ไม่ครบงงอยู่นานมากทำไม่มันมันใช้ไม่ได้[code language=”php” title=”\app\Http\Kernel.php”]

    protected function bootstrappers()
    {
    $bootstrappers = parent::bootstrappers();

    // Add the SiteConfig bootstrapper to the end.
    $bootstrappers[] = ‘Delatbabel\SiteConfig\Bootstrap\LoadConfiguration’;

    return $bootstrappers;
    }
    …[/code]

  7. Incorporate and Run the Migrations โดย run command[code language=”text” title=”command”]
    php artisan vendor:publish –tag=migrations –force
    php artisan migrate[/code]

การติดตั้งเสร็จแล้ว ที่นี้จะทดลองใช้งาน

  1. สร้างไฟล์ \app\Http\Controllers\HomeController.php[code language=”php” title=”\app\Http\Controllers\HomeController.php”]
    <?php

    namespace App\Http\Controllers;

    use App\User;
    use App\Http\Controllers\Controller;

    class HomeController extends Controller {

    /**
    * Show the profile for the given user.
    */
    public function index()
    {

    return ‘title = ‘.config(‘config.title’);

    $config = config()->all();

    return ‘<pre>’. print_r($config ,true).'</pre>’;
    }

    }
    [/code]

  2. เพิ่ม route ในไฟล์ \app\Http\routes.php [code language=”php”]

    Route::get(‘/’, ‘[email protected]’);

    [/code]
  3. ทดลอง set domain ขึ้นมาเทส เปิดไฟล์ C:\Windows\System32\drivers\etc\hosts [code language=”php” title=”host”]

    127.0.0.1 phunsanit.com
    127.0.0.1 pitt.com

    [/code]
  4. เพิ่มข้อมูลทกสอบโดยใช้ [code language=”sql” title=”sql”]
    INSERT INTO `configs` (`id`, `website_id`, `environment`, `group`, `key`, `value`, `type`, `created_at`, `updated_at`) VALUES
    (1, 1, NULL, ‘config’, ‘title’, ‘localhost’, ”, ‘0000-00-00 00:00:00’, ‘0000-00-00 00:00:00’),
    (2, 1, ‘prod’, ‘config’, ‘title’, ‘localhost ON production’, ”, ‘0000-00-00 00:00:00’, ‘0000-00-00 00:00:00’),
    (3, 1, ‘test’, ‘config’, ‘title’, ‘localhost IN TEST SERVER’, ”, ‘0000-00-00 00:00:00’, ‘0000-00-00 00:00:00’),
    (4, 2, NULL, ‘config’, ‘title’, ‘pitt.com’, ”, ‘0000-00-00 00:00:00’, ‘0000-00-00 00:00:00’),
    (5, 2, ‘prod’, ‘config’, ‘title’, ‘pitt.com ON production’, ”, ‘0000-00-00 00:00:00’, ‘0000-00-00 00:00:00’),
    (6, 2, ‘test’, ‘config’, ‘title’, ‘pitt.com IN TEST SERVER’, ”, ‘0000-00-00 00:00:00’, ‘0000-00-00 00:00:00’);

    INSERT INTO `websites` (`id`, `name`, `http_host`, `environment`, `created_by`, `updated_by`, `created_at`, `updated_at`, `deleted_at`) VALUES
    (1, ‘prototype’, ‘localhost’, ‘test’, NULL, NULL, ‘0000-00-00 00:00:00’, ‘0000-00-00 00:00:00’, NULL),
    (2, ‘prototype’, ‘pitt.com’, ‘test’, NULL, NULL, ‘0000-00-00 00:00:00’, ‘0000-00-00 00:00:00’, NULL);
    [/code]

  5. ทดสอบโดยเปิด http://localhost/mcms/public/ และ http://pitt.com/mcms/public/

นำไปดัดแปลงให้เหมาะกับงานที่ทำครับ

สร้าง grid ใน laravel แบบ advance

การใช้ Nayjest Grids แบบ advance เพื่อที่จะสามารถค้นหา filter ข้อมูล, export ไฟล์ ออกมาในรูปแบบ .csv และ excel ได้

ติดตั้งโดย

  1. เปิดไฟล์ composer.json เพิ่ม[code language=”php” title=”composer.json”]

    "laravelcollective/html": "^5",
    "maatwebsite/excel": "~2.1.0",
    "nayjest/grids": "^1.3.1"

    [/code]
  2. Command[code language=”text”]composer update[/code]
  3. เปิดไฟล์ \config\app.php เพิ่ม [code language=”php” title=”configpp.php”]
    ‘providers’ => [

    ‘Collective&#92;Html&#92;HtmlServiceProvider’,
    ‘Maatwebsite&#92;Excel&#92;ExcelServiceProvider’,
    ‘Nayjest&#92;Grids&#92;ServiceProvider’,

    ‘aliases’ => [

    ‘Excel’ => ‘Maatwebsite&#92;Excel&#92;Facades&#92;Excel’,
    ‘Form’ => ‘Collective&#92;Html&#92;FormFacade’,
    ‘Grids’ => ‘Nayjest&#92;Grids&#92;Grids’,
    ‘HTML’ => ‘Collective&#92;Html&#92;HtmlFacade’,
    ‘Input’ => ‘Illuminate&#92;Support&#92;Facades&#92;Input’,

    ][/code]

สร้าง grid โดย

  1. [code language=”php” title=”\app\Http\Controllers\UsersController.php”]
    <?php

    namespace App\Http\Controllers;

    use App\user;
    use HTML;
    use Nayjest\Grids\Components\Base\RenderableRegistry;
    use Nayjest\Grids\Components\ColumnHeadersRow;
    use Nayjest\Grids\Components\ColumnsHider;
    use Nayjest\Grids\Components\CsvExport;
    use Nayjest\Grids\Components\ExcelExport;
    use Nayjest\Grids\Components\FiltersRow;
    use Nayjest\Grids\Components\Filters\DateRangePicker;
    use Nayjest\Grids\Components\HtmlTag;
    use Nayjest\Grids\Components\Laravel5\Pager;
    use Nayjest\Grids\Components\OneCellRow;
    use Nayjest\Grids\Components\RecordsPerPage;
    use Nayjest\Grids\Components\RenderFunc;
    use Nayjest\Grids\Components\ShowingRecords;
    use Nayjest\Grids\Components\TFoot;
    use Nayjest\Grids\Components\THead;
    use Nayjest\Grids\EloquentDataProvider;
    use Nayjest\Grids\FieldConfig;
    use Nayjest\Grids\FilterConfig;
    use Nayjest\Grids\Grid;
    use Nayjest\Grids\GridConfig;

    class UsersController extends Controller
    {

    public function Index()
    {

    $query = (new User)
    ->query();

    $grid = new Grid(
    (new GridConfig)
    ->setDataProvider(
    new EloquentDataProvider($query)
    )
    ->setName(‘UserIndex’) /* grid unque id */
    ->setPageSize(20)
    ->setColumns([
    (new FieldConfig)
    ->addFilter(
    (new FilterConfig)
    ->setOperator(FilterConfig::OPERATOR_LIKE)
    )
    ->setLabel(‘ID’)
    ->setName(‘id’) /* field */
    ->setSortable(true)
    ->setSorting(Grid::SORT_DESC)
    ,
    (new FieldConfig)
    ->addFilter(
    (new FilterConfig)
    ->setOperator(FilterConfig::OPERATOR_LIKE)
    )
    ->setLabel(‘Name’)
    ->setName(‘name’)
    ->setSortable(true)
    ,
    (new FieldConfig)
    ->addFilter(
    (new FilterConfig)
    ->setOperator(FilterConfig::OPERATOR_LIKE)
    )
    ->setLabel(‘Email’)
    ->setName(’email’)
    ->setSortable(true)
    ,
    (new FieldConfig)
    ->addFilter(
    (new FilterConfig)
    ->setOperator(FilterConfig::OPERATOR_LIKE)
    )
    ->setLabel(‘Update Information’)
    ->setName(‘count_update’)
    ->setSortable(true)
    ,
    (new FieldConfig)
    ->setLabel(‘Created’)
    ->setName(‘created_at’)
    ->setSortable(true)
    ,
    (new FieldConfig)
    ->setLabel(‘Last Update’)
    ->setName(‘updated_at’)
    ->setSortable(true),
    ])
    ->setComponents([
    (new THead)
    ->setComponents([
    (new ColumnHeadersRow),
    (new FiltersRow)
    ->addComponents([
    (new RenderFunc(function () {
    return HTML::style(‘js/daterangepicker/daterangepicker-bs3.css’)
    . HTML::script(‘js/moment/moment-with-locales.js’)
    . HTML::script(‘js/daterangepicker/daterangepicker.js’)
    . "<style>
    .daterangepicker td.available.active,
    .daterangepicker li.active,
    .daterangepicker li:hover {
    color:black !important;
    font-weight: bold;
    }
    </style>";
    }))
    ->setRenderSection(‘filters_row_column_created_at’),
    (new DateRangePicker)
    ->setName(‘created_at’)
    ->setRenderSection(‘filters_row_column_created_at’)
    ->setDefaultValue([‘1990-01-01’, date(‘Y-m-d H:i:s’)]),
    (new DateRangePicker)
    ->setName(‘created_at’)
    ->setRenderSection(‘filters_row_column_updated_at’)
    ->setDefaultValue([‘1990-01-01’, date(‘Y-m-d H:i:s’)]),
    ])
    ,
    (new OneCellRow)
    ->setRenderSection(RenderableRegistry::SECTION_END)
    ->setComponents([
    new RecordsPerPage,
    new ColumnsHider,
    (new CsvExport)
    ->setFileName(‘usersExport_’ . date(‘Y-m-d’))
    ,
    (new ExcelExport())
    ->setFileName(‘usersExport_’ . date(‘Y-m-d’))
    ,
    (new HtmlTag)
    ->setContent(‘<span class="glyphicon glyphicon-refresh"></span> Filter’)
    ->setTagName(‘button’)
    ->setRenderSection(RenderableRegistry::SECTION_END)
    ->setAttributes([
    ‘class’ => ‘btn btn-success btn-sm’,
    ]),
    ]),
    ])
    ,
    (new TFoot)
    ->setComponents([
    (new OneCellRow)
    ->setComponents([
    new Pager,
    (new HtmlTag)
    ->setAttributes([‘class’ => ‘pull-right’])
    ->addComponent(new ShowingRecords)
    ,
    ]),
    ])
    ,
    ])
    );
    $grid = $grid->render();

    return view(‘users.index’, [
    ‘grid’ => $grid,
    ]);
    }

    }
    [/code]

  2. สร้าง view \resources\views\users\index.blade.php[code language=”php” title=”\resources\views\users\index.blade.php”]@extends(‘layouts.app’)

    @section(‘title’, ‘Users’)

    @section(‘main-content’) <a href="{{ url(‘/excels/form’) }}">
    <button class="btn btn-success btn-sm"><span class="glyphicon glyphicon-upload"></span> Import Datas</button>
    </a> @if(!empty($text))
    <div class="container">{!! $text !!}</div>
    @endif
    <?=$grid;?>
    @stop[/code]

  3. สร้าง route โดยไปที่ \app\Http\routes.php[code language=”php” title=”\app\Http\routes.php”]

    Route::get(‘/users/index’, ‘[email protected]’);
    …[/code]

ทดลองโดยเรียก /users/index ในเครื่องผมคือ http://localhost/laravel52/public/users/index grid แสดงข้อมูลออกมาแล้วแต่ จะส่งออกเป็นไฟล์ csv หรือ excel ยังไงละ ใจเย็นๆครับ อ่าน สร้าง grid ใน laravel แบบ advance ครับ

สร้าง grid ใน laravel

ตัว laravel จะไม่มี grid ติดตั้งมาให้ในตัวครับ ถ้าจะใช้ต้องลงเอง ผมเลือกใช้ คือ Nayjest Grids เพราะว่าใช้ bootstrapt เป็นพื้นฐานใช้งานง่าย ไม่ยากจนเกินไป สามารถส่งออกเป็นไฟล์ .csv และ excel ได้ ถึงจะไม่ได้เป็นแบบ ajax แต่เจ้าของโครงการก็ประกาศว่าจะทำ และดูจากการ update แล้วถือว่าถี่มากคงอีกไม่นานก็ได้ใช้

ติดตั้งโดย

  1. เปิดไฟล์ composer.json เพิ่ม[code language=”php” title=”composer.json”]

    "laravelcollective/html": "^5",
    "nayjest/grids": "^1.3.1"

    [/code]
  2. Command[code language=”text”]composer update[/code]
  3. เปิดไฟล์ \config\app.php เพิ่ม [code language=”php” title=”\config\app.php”]
    ‘providers’ => [

    ‘Collective&#92;Html&#92;HtmlServiceProvider’,
    ‘Nayjest&#92;Grids&#92;ServiceProvider’,

    ‘aliases’ => [

    ‘Form’ => ‘Collective&#92;Html&#92;FormFacade’,
    ‘Grids’ => ‘Nayjest&#92;Grids&#92;Grids’,
    ‘HTML’ => ‘Collective&#92;Html&#92;HtmlFacade’,
    ‘Input’ => ‘Illuminate&#92;Support&#92;Facades&#92;Input’,

    ][/code]

สร้าง grid โดย

  1. [code language=”php” title=”\app\Http\Controllers\UsersController.php”]
    <?php

    namespace App&#92;Http&#92;Controllers;

    use App&#92;Http&#92;Controllers&#92;Controller;
    use Grids;

    class UsersController extends Controller
    {
    public function index()
    {
    $grid = [
    ‘src’ => ‘App&#92;User’,
    ‘columns’ => [
    ‘id’,
    ‘name’,
    ’email’,
    ‘created_at’,
    ‘updated_at’,
    ],
    ];
    echo Grids::make($grid);
    }

    }
    [/code]

  2. สร้าง route โดยไปที่ \app\Http\routes.php[code language=”php” title=”\app\Http\routes.php”]

    Route::get(‘/users/index’, ‘[email protected]’);
    …[/code]

ทดลองโดยเรียก /users/index ในเครื่องผมคือ http://localhost/laravel52/public/users/index grid แสดงข้อมูลออกมาแล้วแต่ จะส่งออกเป็นไฟล์ csv หรือ excel ยังไงละ ใจเย็นๆครับ อ่าน สร้าง grid ใน laravel แบบ advance ครับ

ติดตั้ง laravel ลั๊ลลาเวล

  1. ก่อนอื่นถ้าไม่มี composer ก็ลงมันก่อน
  2. จากนั้น update เจ้า composer ซะก่อนตามธรรมเนียม รีสตาร์ต เพื่อป้องกันปัญหาเปิด command ออกมาพิมพ์[code language=”text”]
    composer self-update
    [/code]
  3. เพิ่มตัวติดตั้ง laravel ก่อนด้วยคำสั่ง[code language=”text”]
    composer global require "laravel/installer"[/code]
  4. ไปที่ที่จะเก็บงานก่อน[code language=”text”]
    D:
    cd D:\xampp\htdocs
    [/code]
  5. สร้างโปรเจคใหม่ชื่อ laravel52[code language=”text”]
    composer create-project –prefer-dist laravel/laravel laravel52
    [/code] ตอนนี้ไฟล์จะถูกสร้างขึ้นมาใน D:\xampp\htdocs\laravel52 เป็นอันสร้างโครงสร้างสำเร็จ
  6. เชื่อมต่อ database โดยไปที่ file \config\database.php แก้ [code language=”text” title=”configdatabase.php”]
    ‘default’ => env(‘DB_CONNECTION’, ‘mysql’),
    [/code]เป็นชนิดฐานข้อมูลที่ใช้ได้คือ

    • MySQL
    • Postgres
    • SQLite
    • SQL Server

    หลังจากนั้นก็ไปแก้ ในอาเรย์ connections เฉพาะ index ที่ตรงกับค่าของ DB_CONNECTION

  7. สร้างตารางใหม่โดย command[code language=”text”]
    cd laravel52
    php artisan migrate
    [/code]เข้าไปดูใน databse ไม่มีอะไรเกิดขึ้น งง ซิครับ
  8. เป็นเพราะ laravel ใช้ config ในไฟล์ .env แก้[code language=”text” title=”.env”]
    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=homestead
    DB_USERNAME=homestead
    DB_PASSWORD=secret
    [/code] และ command[code language=”text”]
    php artisan migrate
    [/code]อีกครั้ง ถ้า config ถูกต้องจะมีตาราง migrations, password_resets, users ขึ้นมา

การติดตั้งเบื่้อต้นสำเร็จแล้ว

Laravel 5 VS Yii 2 เลือกตัวไหนดี

ช่วงนี้เริ่มโปรเจคใหม่ ด้วยความที่ pm อยากจะให้ใช้ framework ตัวใหม่แทน yii2 เลยเลือกตัวยอดนิยมอีกตัวมาคือ laravel 5 ซึ่งถ้าดูใน trends ที่นิยมๆกันคือ CodeIgniter 3, joomla 3, Laravel 5, wordpress 4, Yii 2 เจ้าลั๊ลลาเวล นี่มาแรงจริงๆ

เพราะว่าคุ้นกับเจ้า yii 2 ที่สุดเลยจะเปรียบเทียบเอาง่ายๆ

  • การติดตั้ง yii2 สามารถใช้ composer ได้ และถ้าไม่มีเน็ตก็โหลดที่เป็น zip ไฟล์มาใช้ได้ แต่ laravel 5 ออกแบบให้ใช้ composer เท่านั้น
  • การใช้ command ทั้งคู่ต้องใช้ command ในการทำสิ่งต่างๆ ตั้งแต่การติดตั้งแต่ laravel 5 จะใช้เยอะกว่ามาก อย่างการ update route cache
  • การ config ตัว Yii2 จะมีความซับซ้อนยิ่งเป็น advance template จะมีการ overwrite ตามลำดับคือ common\config > site\config แล้วยังมีแยก -local สำหรับ config ที่ไม่ต้องการให้ git สนใจ เช่น connection database ในเครื่อง ไม่ต้องการให้ upload เข้าไปใน production แต่คนที่มาจับใหม่ๆ จะงงกับมันมาก ส่วน Laravel 5 ตัว config จะกระจายอยู่หลายที่ ถึงจะมีโพลเดอร์ \config แต่ตัว route ดันอยู่ใน \app\Http\routes.php แต่ config ที่เป็น local อยู่ใน .env ใช้ครั้งแรกก็งงว่าทำไม่ config แล้วมันใช้ไม่ได้
  • ด้าน structure folder ตัว Yii2 จะแยก backend / frontens / common / model / controllers / view แน่นอน เพราะว่าจะเอาไปสร้าง route ส่วน laravel 5 จะบังคับบางส่วนแต่ส่วนใหญ่จะเป็นอิสระตามแต่ท่านๆ จะวางโครงสร้างเอาเอง ขนาด model ก็ไม่บังคับว่าจะไว้ที่ไหน แค่มีตัวอย่าง \app\User.php ไว้ตัวเดียวเท่านั้น
  • ขนาดติดตั้ง Yii2 จะมีแทบทุกอย่างติดมาสำหรับงานเว็บทั่วๆไป ถ้าเอามาทำ api อาจจะต้องเอาบางอย่างที่เกินจำเป็นออก แต่ laravel 5 แทบไม่มีอะไรเลยแม่แต่ login, css, framework, grid list, form generator ต้องมาลงเองผ่านcomposer และ command
  • การลง component ต่างก็แนะนำให้ใช้ composer ทั้งคู่ แต่ การ configure ตัว laravel 5 ส่วนใหญ่จะต้องใช้ command คู่กันไปด้วย และต้องแก้หลายจุดมากกว่า YII ที่มักแค่เพิ่มรายการใน config ในไฟล์ใดไฟล์หนึ่งเท่านั้น
  • การสร้างไฟล์ใหม่ Yii จะมี Gii ให้ถ้าสร้างตารางเสร็จเรียบร้อยแล้ว มันจะสร้างให้ครบทั้ง controller, model, view ส่วน laravel 5 จะต่างกันออกไป เหมือนจะให้สร้างโครงสร้างในตัวมันเองแล้ว command สร้างตารางใน database การสร้าง model มันแทบจะสร้างแค่ชื่อ class ให้เท่านั้น
  • ตัว model ตัว laravel 5 ทำออกมาได้ง่ายมาก เพราะว่าไม่ต้องใส่ field / search condition อะไรเลย ใส่แค่ชื่อตารางมันก็ใช้ insert, delete, update ได้แล้ว (แต่อย่าลืมว่า gii ของ yii สร้างพวกนี้ให้แค่คลิก)
  • ด้านคู่มือมีทั้งคู่ แต่ yii2 ได้เปรียบตรงที่ถ้าดูรายละเอียดถึงระดับ class ทำไว้ละเอียดมาก ไม่ใช้แค่ชื่อ class กับอาร์กิวเมนต์ และจะได้อะไรออกมา แต่หลายๆจุดมีตัวอย่างให้ดูด้วย และเพราะให้มาแบบจัดเต็ม ทำให้มีเยอะจนบางครั้งก็สับสนว่าจะใช้ตัวไหนดี อย่าง grid ก็มีแบบของมันเอง กับแบบ bootstrap การสร้างฟอร์มก็มีให้เลือก สร้างจาก database, array ให้เลือก เป็นทั้งข้อดีและข้อเสีย
  • theme อันนี้ laravel 5 ดีกว่ามากต้องยกให้ blae ในด้านการ reused ส่วนต่างๆ ที่จับ mixed ได้ง่ายๆ กว่ามาก

สรุป ง่ายๆละกัน Yii 2 เหมาะกับงานที่ต้องการไปตามกรอบที่มันมีไว้แล้วจริงๆ เหมาะกับงานทั่วๆไป พวก backend ระบบต่างๆ ส่วน laravel 5 มันเหมาะกับเริ่มงานที่ต้องการปรับแต่งเอง มีเวลาที่จะปรับให้เหมาะกับงานที่ทำ มี tools หรือ library ที่ใช้อยู่เดิมเอาเข้ามาใช้