亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

laravel框架數(shù)據(jù)庫配置及操作數(shù)據(jù)庫示例

 更新時間:2019年10月10日 12:05:57   作者:學知無涯  
這篇文章主要介紹了laravel框架數(shù)據(jù)庫配置及操作數(shù)據(jù)庫,結(jié)合實例形式分析了Laravel數(shù)據(jù)庫的基本配置與操作實現(xiàn)技巧,需要的朋友可以參考下

本文實例講述了laravel框架數(shù)據(jù)庫配置及操作數(shù)據(jù)庫。分享給大家供大家參考,具體如下:

laravel 數(shù)據(jù)庫配置

數(shù)據(jù)庫配置文件為項目根目錄下的config/database.php

//默認數(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.

更多關于Laravel相關內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《php面向?qū)ο蟪绦蛟O計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家基于Laravel框架的PHP程序設計有所幫助。

相關文章

最新評論