laravel 的 Eloquent ORM 查询前50条数据 怎么查

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了laravel 的 Eloquent ORM 查询前50条数据 怎么查相关的知识,希望对你有一定的参考价值。

参考技术A $result = Model::query()
->where(/*查询条件*/)
->offset(0)
->take(50)
->get()

查询关系 Laravel Eloquent ORM

【中文标题】查询关系 Laravel Eloquent ORM【英文标题】:Querying Relations Laravel Eloquent ORM 【发布时间】:2015-01-21 07:43:02 【问题描述】:

我对关系的理解有一个问题,让我们进入正题

我有 3 张桌子,都有合适的型号

receipt
 - id
 - date

items_sold
 - id
 - receipt_id
 - item_id

items
 - id
 - name

我遇到的问题是收据通过 items_sold “有很多”项目,但我不能使用“hasManyThrough”,因为 eloquent 使用了错误的“items_sold”的“id”,即使我手动输入密钥

class Receipt extends Eloquent 

    public function items()
    
        return $this->hasManyThrough('Items', 'ItemsSold', 'receipt_id', 'id');
    


没有办法通过这种方式做到这一点,但我可以找到一个雄辩的关系方法可以帮助我处理这种情况

【问题讨论】:

select items.*, items_sold.receipt_id from items inner join items_sold on "items_sold.id = items.@98765433311" items_sold.receipt_id = 1 这是雄辩的说法,除了这部分“items_sold.id = items.id”应该是“items_sold.item_id” = items.id" 【参考方案1】:

正如 lukasgeiter 所说,添加到模型中的 belongsToMany() 关系将允许您访问相关数据。

还有一点需要注意,如果您将连接表的名称更改为item_reciept,您可以在模型中定义多对多关系,而无需专门指定连接表。当 Laravel 看到 belongsToMany 方法没有指定连接表名称时,它会查找涉及蛇形大小写的两个模型的名称,并以小写字母顺序查找。

您的模型方法是:

class Receipt extends Eloquent

    public function items()
        return $this->belongsToMany('Item');
    


class Item extends Eloquent

    public function reciepts()
        return $this->belongsToMany('Reciept');
    

如果您不想重命名您的 items_sold 表,我可以理解,因为它的特定名称表示它的使用超过了连接表。

将这些关系添加到模型中需要注意的另一件事是,它允许您根据自己的请求执行eager loading,这可能对您的情况有所帮助。

假设您想一次性获取特定收据的所有物品。您可以使用以下请求将所有内容放在一起:

$receiptAndItems = Receipt::with('items')->find($recieptId);

这将返回您的特定收据记录的详细信息和一个键 items 以及该给定收据的所有相关项目记录:

// Your `receipt` record
// this is the output when you add `->toArray()` at the end to make it a bit easier to read

array (size=7)
  'id' => int 2
  'name' => string 'Foo' (length=12)
  'created_at' => string '2014-11-22 16:30:02' (length=19)
  'updated_at' => string '2014-11-22 16:30:02' (length=19)
  'items' => 
    array (size=3)
      0 => 
        array (size=7)
          'id' => int 1
          'name' => string 'Bar' (length=13)
          'created_at' => string '2014-11-22 16:30:02' (length=19)
          'updated_at' => string '2014-11-22 16:30:02' (length=19)
          'pivot' => 
            array (size=2)
              ...
      1 => 
        array (size=7)
          'id' => int 2
          'name' => string 'Baz' (length=20)
          'created_at' => string '2014-11-22 16:30:02' (length=19)
          'updated_at' => string '2014-11-22 16:30:02' (length=19)
          'pivot' => 
            array (size=2)
              ...
      2 => 
        array (size=7)
          'id' => int 3
          'name' => string 'FooBarBaz' (length=18)
          'created_at' => string '2014-11-22 16:30:02' (length=19)
          'updated_at' => string '2014-11-22 16:30:02' (length=19)
          'pivot' => 
            array (size=2)

【讨论】:

没问题。我不想骗你的支票,但我刚刚在我目前的项目中解决了很多问题,所以我想把它付清:) 不用担心。我相信有一天会有人欣赏它:)【参考方案2】:

我很确定您真正需要的是 多对多 关系 here

public function items()
    return $this->belongsToMany('Items', 'items_sold');

你的模型真的被称为“项目”吗?如果它实际上是“Item”,则相应地更改为上述代码中的第一个参数

【讨论】:

它的真名是另一个嘿嘿,我弄错了,但应该是你说的“项目”,我去测试一下,谢谢。 好吧,你说得对,谢谢,我需要 laravel 傻瓜手册 xD 没问题 :) 如果您的问题得到解决,请接受我的回答

以上是关于laravel 的 Eloquent ORM 查询前50条数据 怎么查的主要内容,如果未能解决你的问题,请参考以下文章

Laravel Eloquent ORM查询关系里面的关系

laravel 的 Eloquent ORM 查询前50条数据 怎么查

查询关系 Laravel Eloquent ORM

如何根据 laravel eloquent orm 中的两列进行查询

1+X web中级 Laravel学习笔记——Eloquent ORM查询更新删除新增

Laravel Eloquent ORM 与 Query builder 哪个更好?