Loading

Laravel日常使用总结

数据库

字段当做键值key

集合的keyBy()方法详情:戳这里

创建模型类和数据库迁移文件

php artisan make:model Post -m 

创建控制器和基础的方法

php artisan make:controller PermissionController --resource

子查询

 //子查询1.
SELECT COUNT(*) FROM 
 (SELECT * FROM abc GROUP BY col1) AS a;
//laravel代码
$sub = Abc::where(..)->groupBy(..); // Eloquent Builder instance
$count = DB::table( DB::raw("({$sub->toSql()}) as sub") )
    ->mergeBindings($sub->getQuery()) // you need to get underlying Query Builder
    ->count();
//子查询2.
SELECT 
    `p`.`id`
FROM `products` p 
WHERE `p`.`id` IN (
    SELECT 
        `product_id` 
    FROM `product_category`
    WHERE `category_id` IN ('1', '2')
)
AND `p`.`active`=1
//laravel代码
Products::whereIn('id', function($query){
    $query->select('paper_type_id')
    ->from(with(new ProductCategory)->getTable())
    ->whereIn('category_id', ['1', '2'])
    ->where('active', 1);
})->get();

设置获取session和cookie

session

session(['uid'=>'1','name'=>'王召波']);//设置uid和name
session('uid');//获取uid
//设置name在120分钟后失效
Cookie::queue('name', '王召波, 120);
cookie()->queue('name', '王召波, 120);
//获取name
Cookie::get('name');
//更多cookie操作可以查看这个文件vendor\laravel\framework\src\Illuminate\Cookie\CookieJar.php
//默认cookie是加密的,如果不需要加密在app\Http\Middleware\EncryptCookies.php中添加key
    protected $except = [
        'uid'
    ];

问题总结

env文件配置不起作用或配置数据库一直密码错误

在env文件里面看密码是正确的,然后在程序里面用env()方法查看缺少#之后的东西。解决办法:带有#的值用双引号包住

DB_PASSWORD="ruoshui#2021#"
posted @ 2018-11-30 09:49  王召波  阅读(453)  评论(0编辑  收藏  举报