1. 路由

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Route::any('/', function () {
return view('welcome');
});
/* 路由群组
*/
Route::group(['prefix' => 'member'], function () {
Route::any('test1', [
'as' => 'center', function () {
/*return route('center');
*/
return view('welcome');
}
]);
});
Route::get('info', ['uses' => 'TestController@index']);

2. 控制器

1
2
3
4
5
6
7
8
9
<?php
namespace App\Http\Controllers;
class TestController extends Controller
{
public function index()
{
return view('basic1', ['name' => '哈哈']);
}
}

3. 视图

视图示意图

4. 模型

1
2
3
4
5
6
7
8
9
10
<?php
namespace App\Http\Controllers;
use App\Member;
class TestController extends Controller
{
public function index()
{
return Member::getMember();
}
}
1
2
3
4
5
6
7
8
9
10
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Member extends Model
{
public static function getMember()
{
return 'Member';
}
}

5. 使用DB façade和查询构造器操作数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
class StudentController extends Controller
{
// public function index(){
//使用DB façade操作数据库
$bool = DB::insert('insert into student(name,age) values(?,?)', ['Leroi', 18]);
var_dump($bool);
$num = DB::update('update student
set age = ?
where name = ?',[20,'Leroi']);
var_dump($num);
$students = DB::select('select *
from student');
dd($students);
$num = DB::delete('delete
from student
where id> ?',[0]);
var_dump($num);
//使用查询构造器 //简单插入
$bool = DB::table('student')->insert(['name' => 'Li', 'age' => 18]);
var_dump($bool);
//插入之后获取id
$id = DB::table('student')->insertGetId(['name' => 'LiL', 'age' => 28]);
var_dump($id);
//插入多条数据
$bool = DB::table('student')->insert([['name' => 'Lpi', 'age' => 16], ['name' => 'Lia', 'age' => 108]]);
var_dump($bool);
//更新
$num = DB::table('student')->where('id', 2)->update(['age' => 50]);
$num = DB::table('student')->increment('age', 1);
$num = DB::table('student')->decrement('age', 1, ['name' => '啦啦啦']);
var_dump($num);
//删除
$num = DB::table('student')->where('id', '>=', 4)->delete();
var_dump($num);
/////////多注释几个以防万一/////////$num = DB::table('student')->truncate();
var_dump($num);
//获取表的所有数据
$students = DB::table('student')->get();
dd($students);
$students = DB::table('student')->orderBy('id', 'desc')->where('id', '>=', 2)->get();
dd($students);
//获取结果信息第一条
$students = DB::table('student')->orderBy('id', 'desc')->first();
dd($students);
//多条件查询
$students = DB::table('student')->where('id', '>', 2)->where('id', '<', 4)->get();
dd($students);
$students = DB::table('student')->whereRaw('id>= ? and id < ?', [2, 4])->get();
dd($students);
//pluck
$names = DB::table('student')->pluck('name');
dd($names);
//指定键名
$names = DB::table('student')->pluck('name', 'id');
dd($names);
//select
$students = DB::table('student')->select('id', 'name', 'age')->get();
dd($students);
//chunk
DB::table('student')->orderBy('id', 'desc')->chunk(2, function ($students) {
var_dump($students);
if (condition) {
return false;
}
});
//聚合函数
$num = DB::table('student')->count();
$max = DB::table('student')->max('age');
$min = DB::table('student')->min('age');
$avg = DB::table('student')->avg('age');
$sum = DB::table('student')->sum('age');
}
}

6. 使用ORM进行数据库操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//模型 <?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Student extends Model
{
//指定表名
protected $table = 'student';
//指定id
protected $primaryKey = 'id';
//是否自动维护时间戳
public $timestamps = true;
//指定可以批量赋值的字段
public $fillable = ['name', 'age'];
//指定字段不可以批量赋值
public $guarded = [];
public function getDateFormat()
{
return time();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//操作 <?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use App\Student;
class StudentController extends Controller
{
public function index()
{
//使用ORM进行数据库操作 //全部查找
$students = Student::all();
//根据主键查找
$students = Student::find(1);
//根据主键查找,没有则抛出异常
$students = Student::findOrFail(1);
//使用查询构造器查询
$students = Student::get();
$students = Student::where('id', '>', 1)->orderBy('age', 'desc')->first();
Student::orderBy('id', 'desc')->chunk(2, function ($students) {
var_dump($students);
});
$num = Student::count();
//使用模型新增数据
$student = new Student();
$student->name = '哈哈';
$student->age = 108;
$bool = $student->save();
dd($bool);
//使用模型Create方法新增数据
$student = Student::create(['name' => 'ico', 'age' => 18]);
//firstOrCreate以属性查找用户,若没有则新增
$student = Student::firstOrCreate(['name' => 'ipco']);
//firstOrNew以属性查找用户,若没有则返回新的实例,若要保存可保存实例
$student = Student::firstOrNew(['name' => 'ipcoooo']);
$bool = $student->save();
dd($bool);
//通过模型更新数据
$student = Student::find(1);
$student->name = 'kitty';
$bool = $student->save();
var_dump($bool);
$num = Student::where('id', '>', 2)->update(['age' => 41]);
var_dump($num);
//通过模型删除
$student = Student::find(1);
$bool = $student->delete();
//通过主键删除
$num = Student::destory(1);
$num = Student::destory(2, 3);
$num = Student::destory([2, 3]);
$num = Student::where('id', '>', 1)->delete();
}
}

本文早期发布于个人 CSDN 博客:查看原文

站内搜索

没有找到内容!