【问题标题】:How to check the collection having a relation in laravel?如何检查在 laravel 中有关系的集合?
【发布时间】:2016-09-08 06:33:29
【问题描述】:

我们已经知道, 检查数据类型是数组可以使用phpis_array得到答案, 但是,我们如何检查一个有关系的集合(hasMany)?
这里是代码:
1:

User::where('name','alex')->first();

2:

User::where('name','alex')
    ->with(['article' => function($q){
         $q->where('active', 1);
    }])->first();

一个函数需要接受这些类型的数据来做某事, 我需要知道哪一个有关系

【问题讨论】:

  • 添加一些代码以便更好地理解您的意思。
  • 好的,我去拿。

标签: php laravel collections


【解决方案1】:

我想你指的是getRelations()方法:

$user = User::where('name', 'alex')->first();
// If user has some relations loaded (obviously not here)
if ($user->getRelations()) {

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-01
    • 2014-06-15
    • 2019-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多