欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

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

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

//默認(rèn)數(shù)據(jù)庫(kù)為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文件,

打開(kāi)修改配置參數(shù)

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

修改為本地的數(shù)據(jù)庫(kù)信息:

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=123456

laravel 操作數(shù)據(jù)庫(kù)

建立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會(huì)報(bào)錯(cuò):

(1/1) UnexpectedValueException
The Response content must be a string or object implementing __toString(), "boolean" given.

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

希望本文所述對(duì)大家基于Laravel框架的PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論