phper_李sir

163 经验值

class Blog extends Model
{    
    protected  $table = 'default_table';
    private static $prefix;
    public $timestamps = false;
    public function __construct( $prefix = '' )
    {    
        self::$prefix = $prefix;
        $this->table= self::$prefix . '_blog';
        parent::__construct();        
    }
    
    public function insertOrUpdate($attributes, $prefix = '')
    {
        $ad = new self($prefix);
        return $ad;//这个位置的时候 调用的表名还是带有传进来的前缀的
        if (isset($attributes['id']) && $attributes['id']) {
//            $ad = DB::table($prefix . '_ad_category')->find($attributes['id']);
            $ad = $ad->where(['id'=>$attributes['id']])->first();
            dd($ad);//这个时候查的数据是带前缀的表名,但是对象里面的显示的表名发生了变化
            return $ad;
        }
        $ad->title = $attributes['title'];
        $ad->begin = $attributes['begin'];
        $ad->end = $attributes['end'];
        $ad->img = '';
        $ad->url = $attributes['url'];
        $ad->category_id = $attributes['category_id'];
        $ad->operator_id = $attributes['operator_id'];
        return $ad->save();//这个位置修改的就是未带前缀的表名了。我想知道怎么才能传进来前缀的时候,这个模型里全部使用的是指定的表,而不是在去调用默认的表

    }
    
}