新建mysql视图 命名为:c_good_advantage_views
select `g`.`id` AS `id`,`g`.`cas` AS `cas`,`g`.`company_id` AS `company_id`,`a`.`is_advantage` AS `is_advantage` from (`c_goods` `g` join `c_advantage_goods` `a`) where ((`g`.`cas` = `a`.`cas`) and (`g`.`company_id` = `a`.`company_id`) and (`a`.`is_advantage` = 1))
新建视图Model
php artisan make:model Models/View/GoodAdvantageView
视图模型跟普通的表的模型一样使用
namespace App\Models\Prod;
use App\Models\View\GoodAdvantageView;
use Illuminate\Database\Eloquent\Model;
class Good extends Model
{
protected $guarded = [];
protected $fillable = ['cas','prod_id','name_cn','user_id','user_name','company_id','company_name','status', 'is_advantage'];
public function goodAdvantage(){
return $this->hasOne(GoodAdvantageView::class, 'id','id');
}
}