一、laravel原生sql增删改查
<?php
Route::get('/', function () {
return view('test');
});
Route::get('/home','Home@php');
Route::get('/home/testloop','Home@testloop');
Route::get('/home/querys','Home@querys');
Route::get('/home/updates','Home@updates');
Route::get('/home/insert','Home@insert');
Route::get('/home/delete','Home@delete');
Route::get('/home/item','Home@item');
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
// 1、原生:select查询
public function querys(){
$data['admin_list'] = DB::select('select * from admin limit ,:n',['n'=>1]);
return view('querys',$data);
}
// 修改update
public function updates(){
$res = DB::update('update admin set id=99 where id=:n',['n'=>4]);
var_dump($res);
}
//新增insert
public function insert(){
$res = DB::insert('insert into admin(username,password) values("melinda","123456"),("miya","456789")');
var_dump($res);
}
//删除delete
public function delete(){
$res = DB::delete('delete from admin where id=100');
var_dump($res);
}
//链式操作:查询
public function item(){
// 原生,返回数组
$res = DB::select('select * from admin limit 0,:n',['n'=>1]);
// 链式调用
$res2 = DB::table('admin')->first();
echo '<pre>';
print_r($res);
print_r($res2);
}
}
二、laravel链式查询first方法
- 注意准备事项:
- 1、小皮面板新增数据库,然后在vendor下的.env去修改DB
- 总结:
- laravel原生sql增删改查简单明了,最后别忘记配置路由