连接数据库
配置 app
同级目录下的 .env
文件, 根据自己项目的实际情况配置即可
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=study_laravel
DB_USERNAME=root
DB_PASSWORD=123
运行原生SQL语句
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
class DBController extends Controller{
public function index(){
// 增加:
$result = DB::insert("insert into articles(title,content) values('?','?')", [
'title' => 'test artcile title',
'content' => 'test artcile contents'
]);
dump($result);
// 删除:
$result = DB::delete("delete from articles where id=?", [11]);
dump($result);
// 修改:
$result = DB::update("update articles set title=?,content=? where id=?", [
'updated title', 'updated content', '10'
]);
dump($result);
// 查询:
$data = DB::select("select * from articles where id= ? and title", [10]);
dump($data);
}
}
查询构造器
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
class DBController extends Controller{
public function index(){
// 增加:
$result = DB::table('articles')->insert([
'title' => 'test title',
'content' =&g