laravel速查手册

常用语句

助手函数路径
\vendor\laravel\framework\src\Illuminate\Support\helpers.php
config('app.cdn_domain') 读取配置
env('CDN_DOMAIN') 只在配置文件使用的读取
$path = app_path(); 返回app路径C:\www\laravel\app,
$path = base_path(); 项目根目录,C:\www\laravel
$path = config_path();配置路径C:\www\laravel\config
$path = public_path(); 返回public路径C:\www\laravel\public,路径函数都支持生成文件路径
$path = resource_path('assets/sass/app.scss'); 资源路径;C:\www\laravel\resources\assets/sass/app.scss
$path = storage_path('app/file.txt');文件存储路径C:\www\laravel\storage\app/file.txt
__("lang.test") //本地化,lang是文件,配置'locale' => 'zh-CN' trans("lang.test")只翻译键
e('<html>foo</html>') 输出源代码
 str_contains('This is my name', 'my'); 是否包含某个值
str_limit('我是中国人', 3, '...') 截取字符串 中文3个一个汉字
str_random(40) 随机字符串
asset('img/photo.jpg') 生成完整包含http/https路径 secure_asset()只生成https
url('user/profile', ['id'=>1])http://laravel.api.shanliwawa.top/user/profile/1 secure_url只生成https
url()->full() 当前完整域名
$cookie = cookie('name', 'value', $minutes);
{{ csrf_field() }} 生成令牌 $token = csrf_token(); 获取令牌
encrypt() decrypt() 加密解密
dump()打印变量 dd() 打印后停止
info('Some helpful information!'); 写入日志
logger('Debug message'); 错误写入日志
redirect('/home'); 跳转
$value = session('key'); session(['chairs' => 7, 'instruments' => 3]); 获取设置session
use Illuminate\Support\Facades\Hash;
Hash::make($request->newPassword) 加密密码
助手加密解密函数,支持字符串数组对象
encrypt() decrypt()
use Illuminate\Support\Facades\Crypt;无序列化加密
$encrypted = Crypt::encryptString('Hello world.');
$decrypted = Crypt::decryptString($encrypted);
Crypt::encrypt() 支持字符串数组对象
Crypt::decrypt()
use Illuminate\Http\Request 是请求对象数据处理
$request->input('title'); 获取输入
$site = $request->input('site', 'Laravel学院'); 请求为空取后边值
$request->input('books.0.author') 数组获取单值
$request->json(); 返回JSON数据格式
$request->all(); 全部
$request->except('id');排除
$request->only(['name', 'site', 'domain']); 只获取这些字段
 $request->has('id') 判断id字段是否存在

数据库操作

use Illuminate\Support\Facades\DB;
DB::select("select * from users where name=?",['admin']); //也支持name=:name,[':name'=>'admin']绑定,返回obj
DB::insert('insert into users (id, name) values (?, ?)', [1, 'Dayle']);
$affected = DB::update('update users set votes = 100 where name = ?', ['John']); 返回影响行数
$deleted = DB::delete('delete from users');返回影响行数
DB::statement('drop table users'); 其他无返回语句
DB::transaction(function () { 事务
 DB::table('users')->update(['votes' => 1]);
 DB::table('posts')->delete();
});
$list = DB::table("users")->get()->toArray();//获取全部转换成数组
DB::table("users")->first() 获取一行
DB::table("users")->value('email'); 获取一个字段
DB::table("users")->pluck('email')->toArray(); 获取一列,如果设置两个pluck('name','id') id是键name是值的键值数组
count() 结果集数量 max('price')最大值 min('price')最小值 avg('price')平均数 sum('price')求和
DB::table('users')->select('name', 'email as user_email')->get(); 返回特定字段
whereRaw('price > IF(state = "TX", ?, 100)', [200]) 原生where
orderBy('name', 'desc') inRandomOrder()随机排序
join('contacts', 'users.id', '=', 'contacts.user_id') 内连接
leftJoin('posts', 'users.id', '=', 'posts.user_id') 左连接
groupBy('account_id') 分组 groupBy('first_name', 'status')
having('account_id', '>', 100) 刷选
take(5) 返回5条
->offset(10) ->limit(5) 限制
insert( ['email' => 'john@example.com', 'votes' => 0]); 插入 支持多条数据
$id=DB::table("users")-> insertGetId( ['email' => 'john@example.com', 'votes' => 0]); 插入后返回id
update(['votes' => 1]); 更新
increment('votes', 5);decrement('votes', 5);自增自减
increment('votes', 1, ['name' => 'John']); 支持其他字段更新
DB::table('users')->truncate(); 清空表
DB::table('users')->where('votes', '>', 100)->delete(); 删除 
$users = DB::table('users')->paginate(15); 分页
$users = DB::table('users')->simplePaginate(15); 简单分页只有上一页下一页
$results->total() 总页数
<div class="container">
 @foreach ($users as $user)
 {{ $user->name }}
 @endforeach
</div>
{{ $users->links() }}
作者:zhiqiang原文地址:https://segmentfault.com/a/1190000040398152

%s 个评论

要回复文章请先登录注册