本文實例講述了laravel框架數(shù)據(jù)庫配置及操作數(shù)據(jù)庫。分享給大家供大家參考,具體如下:
laravel 數(shù)據(jù)庫配置
數(shù)據(jù)庫配置文件為項目根目錄下的config/database.php
//默認(rèn)數(shù)據(jù)庫為mysql
'default' => env('DB_CONNECTION', 'mysql'),
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
發(fā)現(xiàn)都在調(diào)用env函數(shù),找到env文件,即根目錄下的.env文件,
打開修改配置參數(shù)
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
修改為本地的數(shù)據(jù)庫信息:
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=123456
laravel 操作數(shù)據(jù)庫
建立student控制器,控制器代碼
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
class StudentController extends Controller
{
//添加
public function addstudent(){
$student = DB::insert('insert into student(name,age,gender) values(?,?,?)',['張三',12,2]);
var_dump($student);//成功返回bloo值true
}
//獲取
public function getall(){
// $student = DB::select('select * from student');
$student = DB::select('select * from student where id>?',[1]);
return $student;//數(shù)組
}
//修改
public function updstudent(){
$student = DB::update('update student set age= ? where name=?',[10,'張三']);
var_dump($student);//成功返回bloo值true
}
//修改
public function delstudent(){
$student = DB::delete('delete from student where id=?',[10]);
var_dump($student);
}
}
注意 laravel中return true會報錯:
(1/1) UnexpectedValueException
The Response content must be a string or object implementing __toString(), "boolean" given.
更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進(jìn)階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Laravel框架的PHP程序設(shè)計有所幫助。
您可能感興趣的文章:- Laravel如何友好的修改.env配置文件詳解
- Nginx中運行PHP框架Laravel的配置文件分享
- Laravel框架環(huán)境與配置操作實例分析
- Laravel 前端資源配置教程
- laravel config文件配置全局變量的例子
- Laravel數(shù)據(jù)庫讀寫分離配置的方法
- Laravel 數(shù)據(jù)庫加密及數(shù)據(jù)庫表前綴配置方法
- laravel-admin自動生成模塊,及相關(guān)基礎(chǔ)配置方法
- laravel 配置路由 api和web定義的路由的區(qū)別詳解
- Laravel5.6框架使用CKEditor5相關(guān)配置詳解
- Laravel配置全局公共函數(shù)的方法步驟
- Laravel5框架自定義錯誤頁面配置操作示例
- laravel配置Redis多個庫的實現(xiàn)方法
- nginx實現(xiàn)一個域名配置多個laravel項目的方法示例
- laravel 框架配置404等異常頁面
- Laravel 5.5官方推薦的Nginx配置學(xué)習(xí)教程
- Laravel Memcached緩存驅(qū)動的配置與應(yīng)用方法分析
- Laravel 5+ .env環(huán)境配置文件詳解