DB门面,orm基础及blade(laravel)
DB门面
原生
// 增加 DB::insert('insert into user (id,name,email) value (?,?,?)',[1,'laravel','1123190009@qq.com']); // 查询 DB::select('select * from user where id=?',[1]); // 更新 该方法返回受影响的行数 DB::update('update user set name="hjy" where id=?',[2]); //删除 该方法返回被删除的行数 表被删除 DB::delete('delete from user'); // 通用语句 DB::statement('drop table user');
构造器
1 /返回行 2 // 增加 3 DB::table('user')->insert( ['id'=1,'name'='yunchaung']); 4 5 //更新 6 DB::table('users')->where('name','Laravel-Academy')->update(['password'=>'123']); 7 //删除 8 DB::table('user')->where('id','>',3)->delete(); 9 10 //事务 11 DB::transaction(function(){ 12 DB::table('user')->update(['id=>1']); 13 DB::table('post')->delete(); 14 }); 15 DB::beginTransaction(); 16 DB::rollBack(); 17 DB::commit();
查询语句构造器
//查询 DB::table('user')->get(); DB::table('user')->where('id',3)->first(); // 指定一个选择字句 $users = DB::table('users')->select('name', 'email')->get(); //原生 $user=DB::table('posts')->select('cate_id',DB::raw('COUNT(id) as num'))->groupBy('cate_id')->get(); //取出某一列(多行) $roles = DB::table('roles')->lists('title', 'name'); // 从单行中取出单列数据 $name = DB::table('users')->where('name', 'John')->pluck('name'); //取出部分数据 DB::table('users')->chunk(100,function($users){ foreach($users as $user){ if($user->name=='yunchuang') return false; echo $user->name.'<br>'; } }); //where运算符 $users = DB::table('users')->where('votes', '>', 100)->orWhere('name', 'John')->get(); $users = DB::table('users')->whereNotBetween('votes', [1, 100])->get(); $users = DB::table('users')->whereNotIn('id', [1, 2, 3])->get(); DB::table('name')->whereNotNull('column')->get(); //动态where字句 $john = DB::table('users')->whereIdAndEmail(2, 'john@doe.com')->first(); $Jane = DB::table('users')->whereNameOrAge('Jane', 22)->first(); //偏移 & 限制 $users = DB::table('users')->skip(10)->take(5)->get(); //select * from users where name = 'John' or (votes > 100 and title <> 'Admin') DB::table('users')->where('name', '=', 'John')->orWhere(function($query) { $query->where('votes', '>', 100)->where('title', '<>', 'Admin'); })->get(); Join //内连接 $users=DB::table('users')->join('posts','users.id','=','posts.user_id')->get(); $users = DB::table('users')->leftJoin('posts','users.id','=','posts.user_id')->get(); $users = DB::table('users')->join('posts',function($join){ $join->on('users.id','=','posts.user_id') ->where('posts.id','>',1); })->get();
Join
//内连接 $users=DB::table('users')->join('posts','users.id','=','posts.user_id')->get(); $users = DB::table('users')->leftJoin('posts','users.id','=','posts.user_id')->get(); $users = DB::table('users')->join('posts',function($join){ $join->on('users.id','=','posts.user_id') ->where('posts.id','>',1); })->get();
Model的基础
// 指定一个自定义的数据表名称 class User extends Model { protected $table = 'users'; } //新增或更改 Student::create(array('key' => 'value')); // 通过属性找到第一条相匹配的数据或创造一条新数据 Student::firstOrCreate(array('key' => 'value')); // 通过属性找到第一条相匹配的数据或实例化一条新数据 Student::firstOrNew(array('key' => 'value')); // 通过属性找到相匹配的数据并更新,如果不存在即创建 Model::updateOrCreate(array('search_key' => 'search_value'), array('key' => 'value')); //填充 Student::fill($attributes); Student::all(); Student::find(1); Student::destroy(1); Student::where('foo', '=', 'bar')->count(); // 输出原始的查询语句 Student::where('foo', '=', 'bar')->toSql();
Blade
// 区块占位 @yield('name') // 可继承内容区块 @section('sidebar') @show // 实现命名为 name 的区块(yield 占位的地方) @section('name') @stop // 扩展布局模板 @extends('layout.name') // 继承父模板内容(@show 的区块内容) @parent // 包含子视图 @include('view.name') // 包含子视图,并传参 @include('view.name', array('key' => 'value')); @forelse($students as $student) {{$stunden->name}} @empty null @endforelse // 三元表达式的简写,以下相当于「$name ? $name : 'Default'」 {{{ $name or 'Default' }}}

浙公网安备 33010602011771号