laravel 操作数据库

建立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;//数组
}

//修改
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.

猜你喜欢

转载自www.cnblogs.com/gyfluck/p/9037337.html