Laravel

1.簡介

Laravel 是一個 Web 應(yīng)用框架, 有著表現(xiàn)力強、語法優(yōu)雅的特點。Web 框架為創(chuàng)建應(yīng)用提供了一個結(jié)構(gòu)和起點,你只需要專注于創(chuàng)造,我們來為你處理細節(jié)。

Laravel 致力于提供出色的開發(fā)體驗,同時提供強大的特性,例如完全的依賴注入,富有表現(xiàn)力的數(shù)據(jù)庫抽象層,隊列和計劃任務(wù),單元和集成測試等等。

本文介紹 laravel 該如何連接使用瀚高數(shù)據(jù)庫。

2.加載驅(qū)動

具體操作步驟請參考 php_pdo_pgsql 接口說明文檔。

3.數(shù)據(jù)庫連接

3.1.數(shù)據(jù)庫準(zhǔn)備

create database laravel;

CREATE TABLE student(
id bigserial NOT NULL,
name character varying(255) NOT NULL,
sex character varying(255) NOT NULL,
age integer NOT NULL,
created_at timestamp without time zone,
updated_at timestamp without time zone,
PRIMARY KEY (id)
);

3.2.數(shù)據(jù)庫配置

環(huán)境變量文件 .env 配置示例

DB_CONNECTION=pgsql
DB_HOST=192.168.100.101
DB_PORT=5866
DB_DATABASE=laravel
DB_USERNAME=sysdba
DB_PASSWORD=Qwer@1234

應(yīng)用配置文件 database.php 配置示例:

使用env函數(shù),表示先從.env文件里面獲取, 如果獲取成功則使用, 如果獲取失敗, 則使用env函數(shù)的第二個參數(shù)

<?php

use Illuminate\Support\Str;

return [

/*
|--------------------------------------------------------------------------
| Default Database Connection Name
|--------------------------------------------------------------------------
| 默認數(shù)據(jù)庫連接為mysql,可以在.env文件中修改DB_CONNECTION的值
*/

'default' => env('DB_CONNECTION', 'pgsql'),

/*
|--------------------------------------------------------------------------
| Database Connections
|--------------------------------------------------------------------------
| 針對不同數(shù)據(jù)庫相關(guān)配置,包括sqlite、mysql、Postgres、SQL Server
*/

'connections' => [

...

'pgsql' => [
'driver' => 'pgsql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
'schema' => 'public',
'sslmode' => 'prefer',
],

],

];

3.3.增加數(shù)據(jù)庫記錄

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class StudentController extends Controller
{
/**
* 數(shù)據(jù)寫入
*
* @return Response
*/
public function insert()
{
DB::insert("insert into student(name, sex, age) values (?,?,?)",['李四', '女','20']);
DB::insert("insert into student(name, sex, age) values (?,?,?)",['王五', '男','18']);
$res="數(shù)據(jù)寫入成功!";
echo $res;
}
}

3.4.查詢數(shù)據(jù)庫記錄

/**
* 列表查詢
*/

public function select()
{
$students = DB::select('select * from student');
echo json_encode($students);
}

3.5.更新數(shù)據(jù)庫記錄

/**
* 更新數(shù)據(jù)
*/

public function update()
{
DB::update("update student set age=30 where name='李四'");
$res="數(shù)據(jù)更新成功!";
echo $res;
}

3.6.刪除數(shù)據(jù)庫記錄

/**
* 刪除數(shù)據(jù)
*/

public function delete()
{
DB::delete("delete from student where name='王五'");
$res="數(shù)據(jù)刪除成功!";
echo $res;
}

4.常見問題

1)could not find driver報錯

問題原因:php未成功加載pdo_pgsql擴展

解決方案:php.ini 文件,檢查pdo_pgsql是否加載成功

**2)SQLSTATE[08006] [7] authentication method 13 not supported **

問題原因:瀚高數(shù)據(jù)庫支持的認證密碼加密方式包括 md5, sm3 和 scram-sha-256 ,報錯是因為使用了sm3,驅(qū)動不支持國密算法

解決方案一:數(shù)據(jù)庫密碼認證方式修改為 md5 或 scram-sha-256

解決方案二:下載替換對應(yīng)版本的驅(qū)動,具體操作方法請參考 php_pdo_pgsql 接口說明