在我的应用程序中,我有几个 mysql 表:多伦多、温哥华、蒙特利尔等...并且我使用 DB 类来处理它们,例如。
$data = DB::select('select * from toronto where id = ?', array($id));
我想做的是开始使用 Eloquent。我是 Laravel 的新手,只是想知道是否可以让一个模型与多个表一起使用,例如:
class City extends Eloquent {
protected $table_a = 'toronto';
protected $table_b = 'vancouver';
protected $table_c = 'montreal';
}
它不能,但你可以。方法有很多,这里是一种:
创建一个城市模型,在其构造函数中要求输入表名:
class City extends Eloquent {
public function __construct($city, array $attributes = array())
{
parent::__construct($attributes);
$this->table = $city;
}
}
要使用它,您必须使用表名称实例化您的类:
$toronto = new City('toronto');
然后你可以用它做任何你想做的事情:
var_dump( $toronto->where('id',701057)->get() );
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)