我们已经知道检查数据类型是一个数组可以使用php is_array
得到答案,但是,我们如何检查具有关系(hasMany)的Collection ?下面是代码:
1:
User::where('name','alex')->first();
2:
User::where('name','alex')
->with(['article' => function($q){
$q->where('active', 1);
}])->first();
一个函数需要接受这些类型的数据来做一些事情,我需要知道哪一个有关系
我想你指的是getRelations()
方法:
$user = User::where('name', 'alex')->first();
// If user has some relations loaded (obviously not here)
if ($user->getRelations()) {
}