---
# System prepended metadata

title: PHP LARAVEL

---

# PHP LARAVEL

**1. Chuẩn bị**
* Composer.
https://getcomposer.org/download/
* Môi trường PHP (xampp).
https://www.apachefriends.org/download.html

**2. Cài đặt**
* Tạo project:
    `composer create-project laravel/laravel=8 laravel`
* Run server:
    ```
    php artisan serve
    hoặc
    php artisan serve --host=192.168.1.60 --port=8000
    ```
* Tạo thư mục storage và liên kết với nó
    `php artisan storage:link`
* Tạo migration bảng 
    `php artisan make:migration create_product_table --create=product`
* Tạo seeder bảng 
    `php artisan make:seeder
    ProductSeeder
    `
* Chạy backup lại migration và seeder đồng loạt
    `php artisan migrate:fresh --seed`
* Tạo model 
    `php artisan make:model Product`
* tạo .evn và gender token key
    `php artisan key:generate`

**3. Config HOST**
* Path: C:\Windows\System32\drivers\etc 
* Host: 192.168.1.60 laravelApp

**4. EXTENTION**
* Laravel Artisan --- *quản lý cache, DB, general key*
* Laravel Snippets 
* Laravel Blade Snippets --- *Auto Div.class*
* Laravel Blade Spacer --- *Blade object {{ }}* 
* Laravel goto view
* laravel-jump-controller
* Live Sass Compiler --- *Live css save*
* Vscode-icon --- *Change icon folder*
* Auto Close Tag
* PHP Formatter

**5. TRICK**
* Xem info version php
    `return phpinfo();`
    
* Chỉ ra giá trị default nếu không thỏa
    `b =  a ??  'null'`
    
* @unless = ifnot
* Kiểm tra route hiện tại trong blade view
    `{{ Request::routeIs('home') ? 'active' : '' }}`
    
* Cách sử dụng biến trong .env (define environment variable)
    `return env('NAME')`
    
* Dùng lib Carbon format datetime trong blade view
    `{{ \Carbon\Carbon::parse($item->TIME_END)->format('H:m') }}`
    
* Url domain path: URL()
    `<img src={{ URL('img/vituanAvt.jpg') }} >`
    
* whereNotNull('col')

* Cấu hình group_by elequoent 1 field -- *In mysql array, set to disable MySQL's strict mode*
    ```
    config/database.php
    strict => false 
    ```

* Xử lý Ảnh
    ```
    $request->file('image')
    ->guessExtension()); --- jpg, png lấy ra đuôi mở rộng
    ->getMineType --- image.jpg phải là file ảnh
    ->getClientOriginalName() --- lấy tên gốc file
    ->getSize() --- lấy kích thước file
    ->getError() --- trả về lỗi
    ->isValid 
    ```
**6. PHP DEBUG**
* Cài đặt Xdebug với php_info()
    https://xdebug.org/wizard
* Dán đoạn sau vào cuối file php.ini
    ```
    [XDebug]
    xdebug.mode=debug
    xdebug.start_with_request=yes
    xdebug.client_port=9003
    xdebug.log_level=7
    xdebug.idekey=VSCODE
    xdebug.client_host="192.168.1.60"
    zend_extension="C:\xampp\php\ext\php_xdebug.dll"
    ```
**7. SCSS , LARAVEL MIX**
```
webpack.mix.js

mix.js('resources/js/app.js', 'public/js')
    .sass('resources/scss/app.scss', 'public/css', [
        //
    ]);


type package.json

npm i

npm run watch
```

**8. Temp**
```
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class NameIPController extends Controller
{
    public static function getUserIpAddr(){
        $ipaddress = '';
        if (isset($_SERVER['HTTP_CLIENT_IP']))
            $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
        else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
            $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
        else if(isset($_SERVER['HTTP_X_FORWARDED']))
            $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
        else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
            $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
        else if(isset($_SERVER['HTTP_FORWARDED']))
            $ipaddress = $_SERVER['HTTP_FORWARDED'];
        else if(isset($_SERVER['REMOTE_ADDR']))
            $ipaddress = $_SERVER['REMOTE_ADDR'];
        else
            $ipaddress = 'UNKNOWN'; 
        return  $ipaddress;   
    }
}

$ipaddress =  DeviceIP::getUserIpAddr();
```
**9. LIBRARY**
1. **Guzzle**: là một PHP HTTP client giúp việc gửi HTTP request trở nên đơn giản "Mail"

    `composer require guzzlehttp/guzzle`
2. Thanh toán online
```
B1: composer require stripe/stripe-php  // payment install

B2: composer require laravel/cashier

B3: khai bao service
'stripe' => [
        'secret' => env('STRIPE_SECRET'),
        'key' => env('STRIPE_KEY'),
        'webhook_secret' => env('STRIPE_WEBHOOK_SECRET'),
    ],
B4: .evn khai bao secret key
STRIPE_KEY 
STRIPE_SECRET

Example Visa:
VISA
4242424242424242
232
03/27

get example card to payment
https://stripe.com/docs/testing?testing-method=card-numbers#cards
pass: vituan@1397

xem danh sách thanh toán 
https://dashboard.stripe.com/test/payments
```
3. Excel

```
composer require phpoffice/phpspreadsheet

use Illuminate\Support\Facades\Session;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;
```
