Home  >  Q&A  >  body text

get() all()的区别?

$students=student::get();//获取模型所有数据?
dd($students);

$students=student::all();//查询模型所有数据?
dd($students);


这两者有什么区别呢?


phpcn_u266phpcn_u2662812 days ago1277

reply all(2)I'll reply

  • 数据分析师

    数据分析师2017-09-30 23:53:49

    What is the difference between get() all()? - PHP Chinese website Q&A - What is the difference between get() all()? - PHP Chinese website Q&A

    Take a look around and learn.

    reply
    0
  • 阿神

    阿神2017-01-11 11:45:25

    查阅Model源码可得

    public static function all($columns = ['*'])
    {
        $columns = is_array($columns) ? $columns : func_get_args();
        $instance = new static;
        return $instance->newQuery()->get($columns);
    }

    而newQuery()得到的是是一个Builder类

    all()不传参有默认值*,继而调用get();

    get()位于Illuminate\Database\Eloquent\Builder中

    同样有默认值*

    public function get($columns = ['*'])
    {
        $builder = $this->applyScopes();
        $models = $builder->getModels($columns);
        
        if (count($models) > 0) {
            $models = $builder->eagerLoadRelations($models);
        }
        return $builder->getModel()->newCollection($models);
    }

    故得证

    reply
    0
  • Cancelreply