Php Laravel雄辩而急切地加载,很少有列与一个表相关
我从数据库中获得如下模型结构:Php Laravel雄辩而急切地加载,很少有列与一个表相关,php,laravel,laravel-5,eloquent,laravel-5.4,Php,Laravel,Laravel 5,Eloquent,Laravel 5.4,我从数据库中获得如下模型结构: class Player extends Model { protected $table = 'players'; } 与玩家相关(此表始终必须有5个固定位置): 当我尝试像这样加载FormationPositions表时: class Player extends Model { protected $table = 'players'; } FormationPositions::with(['fpos1','fpos
class Player extends Model
{
protected $table = 'players';
}
与玩家相关(此表始终必须有5个固定位置):
当我尝试像这样加载FormationPositions
表时:
class Player extends Model
{
protected $table = 'players';
}
FormationPositions::with(['fpos1','fpos2','fpos3','fpos4','fpos5'])
然后我对数据库进行了5次查询以加载这些对象,也许有某种方法可以将此结构与Elequent Earge way组合成一个查询
SELECT*FROM table where id IN(?)
,并通过一次查询将数据加载到该对象?您可以添加:protected$with=['fpos1',fpos2',fpos3',fpos4',fpos5']代码>在您的播放器模型中始终加载:),您将获得5个数据库查询,而不是1您是否可以共享多一点您的模式?在我看来,这将是重构数据结构的一个好例子。