Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel模型对象检索关系模型,带where条件,不带“where”;获得;方法_Php_Laravel_Octobercms - Fatal编程技术网

Php Laravel模型对象检索关系模型,带where条件,不带“where”;获得;方法

Php Laravel模型对象检索关系模型,带where条件,不带“where”;获得;方法,php,laravel,octobercms,Php,Laravel,Octobercms,我正在研究octoberCMS(laravel框架),我对检索where子句上的关系模型有疑问 我已经根据主键“id”从“clothing type”模型中获取了一个服装类型记录 这个“服装类型”模型与“产品”模型相关,关系是=>每个服装类型都有许多产品 万事如意;现在我的业务逻辑有两种情况,一种是获取服装类型的所有产品,另一种是获取服装类型的第一个产品。因此,我使用$clothing\u type->products获取所有产品,并使用$clothing\u type->products->f

我正在研究octoberCMS(laravel框架),我对检索where子句上的关系模型有疑问

我已经根据主键“id”从“clothing type”模型中获取了一个服装类型记录

这个“服装类型”模型与“产品”模型相关,关系是=>每个服装类型都有许多产品

万事如意;现在我的业务逻辑有两种情况,一种是获取服装类型的所有产品,另一种是获取服装类型的第一个产品。因此,我使用$clothing\u type->products获取所有产品,并使用$clothing\u type->products->first()获取第一个产品

现在我必须对这两种情况都应用一个条件。条件是只获取状态为“活动”的产品,因此
$products=$TANGOUS\U type->products->where('status','Active')
$first\u product\u detail=$products->first()

每件事都按预期进行,但是为什么没有“get()”方法获取产品呢$服装类型->产品->位置('status','Active')->get();由于我是新的关系,我想知道这是如何工作的,或者这是一个坏的方式来获取记录或不正确的假设。但一切都很好

$clothing_type  = ClothingType::where('id',post('clothingtype_id'))->where('status','Active')->first();
if(count($clothing_type->products))
{
    $products               = $clothing_type->products->where('status','Active');
    $first_product_detail   = $products->first();

}

没有什么可怕的

first()
where()

illumb\Database\Query\Builderillumb\Support\Collection的函数,所有
首先
做的是将记录限制为1,然后给您第一条记录。当您使用生成器时,将进行查询以获取1条记录,并在集合上使用它,所有记录都将首先获取(),然后返回其中的第一条记录

这里,

当你这样做的时候

$clothing\u type->products
,Laravel为您提供了一系列产品

所以

$products
illumb\Support\Collection

$products->first()
调用该类中的
first()
函数


有关收藏的文档和方法…

您的做法是正确的。当您作为属性访问关系时,Elount会自动检索记录

但是,如果将关系作为方法访问,则会得到查询本身,您可以向其中添加过滤器:

if(count($clothing_type->products))
{
    $products               = $clothing_type->products()->where('status','Active')->get();
    $first_product_detail   = $products->first();
}
这会解决你的问题

()

编辑:还要注意的是,第一个方法不是雄辩的方法,而是来自集合,它非常强大

编辑2: 我误解了你问题中你想知道这是怎么可能的部分。雄辩和收藏都有where方法。我想你理解雄辩的作品,但这本藏书中的作品几乎是一样的()

我自己更喜欢雄辩的,因为这限制了从数据库检索到的记录数量。但是如果您以后在代码中需要所有产品(甚至是非活动产品),只需使用Collection方法过滤掉活动产品即可

if(count($clothing_type->products))
{
    $products               = $clothing_type->products()->where('status','Active')->get();
    $first_product_detail   = $products->first();
}