模型自定义了方法
假如我在模型自定义了2个方法
class BaseModel extends Model implements CacheableInterface
{
use Cacheable;
public $timestamps = false;//关闭自动写入时间戳
public string $alias = '';
public function withSearch(array $data, array $where = [], array $ignore = ['all'])
{
$builder= $this->xxxx();
return $builder;
}
public function alias($alias)
{
$this->alias = $alias;
return $this->from($this->getTable() . ' as ' . $alias);
}
}
$a = (new mode())->alias('a')->withSearch();
这个时候就会提示Builder找不到withSearch,所以我改成了
假如我在模型自定义了2个方法
class BaseModel extends Model implements CacheableInterface
{
use Cacheable;
public $timestamps = false;//关闭自动写入时间戳
public string $alias = '';
public function withSearch(array $data, array $where = [], array $ignore = ['all'])
{
$builder= $this->xxxx();
return $builder;
}
public function alias($alias)
{
$this->alias = $alias;
$this->from($this->getTable() . ' as ' . $alias);
return $this;
}
}
$a = (new mode())->alias('a')->withSearch();
这样的话from就无法生效了
可考虑使用宏指令来扩展,新建
App\Providers\MacrosServiceProvider::class
,记得添加到config/app.php
的providers
加载。