在基础模型里 1classBaseModelextends\Eloquent{2publicfunctionscopeWithOnly($query,$relation,Array$columns)3{4return$query->with([$relation=>function($query)use($columns){5$query->select(array_merge(['id'],$columns));6}]);7}8} 在我们普通的 Model 类都继承基类: 1classUserextendsBaseModel{2...
接下来,可以使用Query Builder执行查询操作,并将列名作为结果返回。例如,查询"users"表的列名: 代码语言:txt 复制 $columns = DB::select(DB::raw('SHOW COLUMNS FROM users')); $columnNames = array_column($columns, 'Field'); return $columnNames; 上述代码中,使用DB::select()方法...
return function (array $columns, $values) { /** @var \Illuminate\Database\Eloquent\Builder $this */ return $this->whereIns($columns, $values, 'or', true); }; } } AppServiceProvider注册QueryBuilderMacro即可 <?php namespace App\Providers; use App\Support\Macros\QueryBuilderMacro; use Ill...
1 Getting specific columns from Laravel Eloquent 20 How to Select Certain Fields in Laravel Eloquent? 13 Select multiple column form database in Laravel 8 Select specific columns using find in Eloquent ORM 2 How to select specific columns with eloquent "with" 1 How to select some columns...
classBaseModelextends\Eloquent{publicfunctionscopeWithOnly($query,$relation,Array$columns) {return$query->with([$relation=>function($query)use($columns){$query->select(array_merge(['id'],$columns)); }]); } } 在我们普通的 Model 类都继承基类: ...
* Execute the query as a "select" statement. * *@paramarray $columns *@returnarray|static[] */publicfunctionget($columns = ['*']) {if(is_null($this->columns)) { $this->columns = $columns; }return$this->processor->processSelect($this, $this->runSelect()); ...
* Execute the query as a "select" statement. * * @param array $columns * @return array|static[] */ public function get($columns = ['*']) { if (is_null($this->columns)) { $this->columns = $columns; } return $this->processor->processSelect($this, $this->runSelect()); ...
public static function all($columns = ['*']) { return (new static)->newQuery()->get( is_array($columns) ? $columns : func_get_args() ); } 这个查询过程,可以分成三个步骤来执行: new static: 模型实例化,得到模型对象。 $model->newQuery(): 根据模型对象,获取查询构造器$query。
* Execute the query as a "select" statement. * * @param array $columns * @return array|static[] */ public function get($columns = ['*']) { if (is_null($this->columns)) { $this->columns = $columns; } return $this->processor->processSelect($this, $this->runSelect()); ...
public function first($columns = ['*']) { $results = $this->take(1)->get($columns); return count($results) > 0 ? reset($results) : null; } /** * Execute the query as a "select" statement. * * @param array $columns