Laravel源码阅读第一次

Builder::select

源码
public function select($columns = ['*'])
{
    $this -> columns = is_array($columns) ? $columns : func_get_args();
    //设置被选择的字段
    //如果被选择的字段是一个数组,那么设置当前query的clumns为这个数组
    //如果被选择的字段不是一个数组,用func_get_args把这些参数变成一个数组
    return $this;
}
示例
$builder = User::where("id", 160124) -> select("id", "realname", "nickname");
$builder = User::where("id", 160124) -> select(["id", "realname", "nickname"]);
dump($builder->get());
代码分析

首先让我感到疑惑的是,为什么函数的形参可以和传递过来的实参不一一对应。于是我发现了func_get_args()的作用。
func_get_args():获取函数的所有参数。
func_get_arg($index):传递一个下标,可以获得参数列表中对应的参数。
func_num_args():获取函数参数的个数。

function foo(){
    $args_num = func_num_args();
    echo "Number of arguments: $args_num<br/>\n";
    $args_list = func_get_args();
    for ($i =0; $i < $args_num; $i++) {
        echo "Argument $i is: ".func_get_arg($i)."<br/>";
    }
}
foo(1,2,3);

输出

Number of arguments: 3
Argument 0 is: 1
Argument 1 is: 2
Argument 2 is: 3

Builder::addSelct,Builder::selectRaw

两个函数具有相同作用

源码
public function addSelect($column)
{
    //往被选择的字段添加字段,即扩充被选择字段
    //参数支持数组和多个字符串
    $column = is_array($column) ? $column : func_get_args();

    $this->columns = array_merge((array) $this->columns, $column);

    return $this;
}
示例
$builder = User::where("id", "160124") ->select("id", "realname") -> selectRaw("nickname");
$builder = User::where("id", "160124") ->select(["id"]) -> addSelect(["nickname", "realname"]);
dump($builder->get());

selectSub

完全看不懂在说什么,o(╯□╰)o,可以参考以下信息。
StackOverFlow参考

distinct

据说是去重,但是不知道怎么用。

源码
public function distinct(){   
    //Force the query to only return distinct results. 
    $this->distinct = true;    
    return $this;
}

在Laravel中想要针对某个字段去重可以使用unique
unique方法返回集合中所有的唯一数据项:

$collection = collect([1, 1, 2, 2, 3, 4, 2]);
$unique = $collection->unique();
$unique->values()->all();
// [1, 2, 3, 4]

返回的集合保持原来的数组键,在本例中我们使用values方法重置这些键为连续的数字索引。
处理嵌套数组或对象时,可以指定用于判断唯一的键:

$collection = collect([
    ['name' => 'iPhone 6', 'brand' => 'Apple', 'type' => 'phone'],
    ['name' => 'iPhone 5', 'brand' => 'Apple', 'type' => 'phone'],
    ['name' => 'Apple Watch', 'brand' => 'Apple', 'type' => 'watch'],
    ['name' => 'Galaxy S6', 'brand' => 'Samsung', 'type' => 'phone'],
    ['name' => 'Galaxy Gear', 'brand' => 'Samsung', 'type' => 'watch'],
]);
$unique = $collection->unique('brand');
$unique->values()->all();

/*
    [
        ['name' => 'iPhone 6', 'brand' => 'Apple', 'type' => 'phone'],
        ['name' => 'Galaxy S6', 'brand' => 'Samsung', 'type' => 'phone'],
    ]
*/

你还可以指定自己的回调用于判断数据项唯一性:

$unique = $collection->unique(function ($item) {
    return $item['brand'].$item['type'];
});
$unique->values()->all();

/*
    [
        ['name' => 'iPhone 6', 'brand' => 'Apple', 'type' => 'phone'],
        ['name' => 'Apple Watch', 'brand' => 'Apple', 'type' => 'watch'],
        ['name' => 'Galaxy S6', 'brand' => 'Samsung', 'type' => 'phone'],
        ['name' => 'Galaxy Gear', 'brand' => 'Samsung', 'type' => 'watch'],
    ]
*/
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,854评论 18 139
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,498评论 0 17
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,742评论 18 399
  • (1)(另)另一种记法 (2)(串)一句话把不同的意思记到一起 (3) (搭) 一个词的常用搭配 (4)(变)一个...
    八月时光阅读 368评论 0 0
  • 现在Android 项目中基本都是MVP或MVVM架构的,相比之前的MVC架构MVP需要为Presenter层创建...
    水瓶鱼阅读 1,338评论 0 3