1 回答
TA贡献1845条经验 获得超8个赞
您可以尝试一下:创建一个新的构建器和一个使用这个新构建器的特征:
class BuilderWithSpecifiedColumns extends Builder
{
public $selectedColumns = [];
public function __construct(ConnectionInterface $connection, Grammar $grammar = null, Processor $processor = null, array $selectedColumns = ['*'])
{
parent::__construct($connection, $grammar, $processor);
$this->selectedColumns = $selectedColumns;
}
/**
* @param string[] $columns
* @return \Illuminate\Support\Collection
*/
public function get($columns = ['*'])
{
return parent::get($this->selectedColumns ? $this->selectedColumns : $columns);
}
}
trait HasSelectedColumns
{
protected function newBaseQueryBuilder()
{
$connection = $this->getConnection();
return new BuilderWithSpecifiedColumns(
$connection,
$connection->getQueryGrammar(),
$connection->getPostProcessor(),
$this->selectedFieldsThatMeanSelectStarDoesntHappen,
);
}
}
使用以上特征
/**
* @property mixed id
*/
class Attribute extends Model
{
use HasSelectedColumns;
protected $fillable = ["id", "business_id", "attribute_name"];
protected $with = ["attributeDetail", "business"];
protected $selectedFieldsThatMeanSelectStarDoesntHappen = ["id", "business_id", "attribute_name"];
}
- 1 回答
- 0 关注
- 91 浏览
添加回答
举报