来自collation\App\CourseStart的php laravel调用函数->;查找(任何)——>;学生->;带有连接的count()

来自collation\App\CourseStart的php laravel调用函数->;查找(任何)——>;学生->;带有连接的count(),php,laravel,Php,Laravel,首先,我可以编写这个\App\CourseStart->find(any)->students->count(),并且可以很好地工作 put我被迫连接相同的变量$object->$k->count() $object=(`\App\CourseStart->find(any))排序规则laravel App\CourseStart{#1309▼ #日期:数组:2[▶] #可填充:数组:15[▶] #连接:“mysql” #表:“课程开始” #主密钥:“id” #键类型:“int” +递增:真 #

首先,我可以编写这个
\App\CourseStart->find(any)->students->count()
,并且可以很好地工作 put我被迫连接相同的变量
$object->$k->count()

$object=(`\App\CourseStart->find(any))排序规则laravel
App\CourseStart{#1309▼
#日期:数组:2[▶]
#可填充:数组:15[▶]
#连接:“mysql”
#表:“课程开始”
#主密钥:“id”
#键类型:“int”
+递增:真
#带:[]
#withCount:[]
#每页:15
+存在:正确
+最近被创建:false
#属性:数组:6[▶]
#原件:阵列:6[▶]
#更改:[]
#演员:[]
#classCastCache:[]
#日期格式:空
#附件:[]
#调度事件:[]
#可观测值:[]
#关系:数组:1[▶]
#触摸:[]
+时间戳:真
#隐藏:[]
#可见:[]
#防护:阵列:1[▶]
#强制删除:false
}
$k=‘学生’是关系
所以$object->{$k}=\App\CourseStart->find(any)->学生这项工作100%
照亮\数据库\雄辩\收藏{#1310▼
#项目:阵列:2[▼
0=>App\Student{#1311▶}
1=>App\Student{#1314▶}
]
}
$object->{$k}->count();
工作获取结果“2”

$f=['count()','','',]

现在我试着写这个
$object->{$k}->$f[0]
$object->$k->{$f[0]}

我有这个错误

Exception
Property [count] does not exist on this collection instance.
现在,如何将函数count()连接到$object->{$k}

不确定哪个地址适合这个问题?

经过一些实验

$f = ['count','','']// remove brackets of the functions
$f2 = $f[0];// assign to a variable for extracting the function from the array
$object->{$k}->$f2()// this will work
资料来源:

我在我的一个项目中测试了它,作为至爱:

$x = Page::where ( 'slug', $page_slug );
        $fA = ['count','get',''];
        $f = $fA[0];
        dd( $x->$f());