Php Laravel单表模型继承
我有一篇文章,它通过Post、News等进行了扩展。当我调用这些子类上的all等雄辩方法时,我希望它们通过_类_来过滤结果。classname的复数、小写版本将存储在articles表中的某个枚举字段类型中。目前,我所得到的一切,包括新闻文章 是否有特定于Laravel的解决方案,我不必覆盖整个雄辩的功能 文章 新闻 //我使用的是Laravel5,MasterBranch的最新开发版本Php Laravel单表模型继承,php,laravel,eloquent,Php,Laravel,Eloquent,我有一篇文章,它通过Post、News等进行了扩展。当我调用这些子类上的all等雄辩方法时,我希望它们通过_类_来过滤结果。classname的复数、小写版本将存储在articles表中的某个枚举字段类型中。目前,我所得到的一切,包括新闻文章 是否有特定于Laravel的解决方案,我不必覆盖整个雄辩的功能 文章 新闻 //我使用的是Laravel5,MasterBranch的最新开发版本 发现Laravel有一个内置函数newQuery来覆盖QueryBuilder功能。这让事情变得容易多了 &
发现Laravel有一个内置函数newQuery来覆盖QueryBuilder功能。这让事情变得容易多了
<?php
namespace App;
use Illuminate\Database\Eloquent\Model as Eloquent;
abstract class Article extends Eloquent{
protected $table = 'articles';
public function __construct($attributes = array()){
parent::__construct($attributes);
$this->setAttribute('class_name',get_class($this));
}
public function newQuery($excludeDeleted = true){
$builder = parent::newQuery($excludeDeleted);
if ('Article' !== get_class($this)) {
$c = get_class($this);
$c = explode('\\', $c);
$c = end($c);
$builder->where('type',"=", $c);
}
return $builder;
}
}
我需要分解,因为名称空间符号。全局范围是Laravel方法-
<?php
namespace App\Article;
class News extends \App\Article{
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model as Eloquent;
abstract class Article extends Eloquent{
protected $table = 'articles';
public function __construct($attributes = array()){
parent::__construct($attributes);
$this->setAttribute('class_name',get_class($this));
}
public function newQuery($excludeDeleted = true){
$builder = parent::newQuery($excludeDeleted);
if ('Article' !== get_class($this)) {
$c = get_class($this);
$c = explode('\\', $c);
$c = end($c);
$builder->where('type',"=", $c);
}
return $builder;
}
}