laravel 4 - 如何限制(采取和跳过)雄辩的ORM? [英] laravel 4 - how to Limit (Take and Skip) for Eloquent ORM?

查看:106
本文介绍了laravel 4 - 如何限制(采取和跳过)雄辩的ORM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL; DR



可以限制一个口述的ORM查询,如使用 take() skip(),以便生成的mysql查询也受到限制,并且不必返回整个数据集?



如果是这样,你如何修改:

  $ test = User :: find(1) - > games-> toArray (); 

包含 limit 3 offset 2






表:



 用户游戏userGames 
- id - id - user_id
- 名称 - 名称 - game_id
- steam_id
pre>

型号:



  class User Enloquent {
public function games(){
return $ this-> belongsToMany('Game','userGames','user_id','game_id');
}
}

类游戏扩展Eloquent {
public function users(){
return $ this-> belongsToMany('User',' userGames','user_id','game_id');
}
}






查询构建器中的限制



使用常规的Laravel 查询构建器,我可以获得所有游戏属于id 1的用户,并将结果限制为 take() skip ()

  $ test = DB :: table('games')
- > join('userGames','userGames.game_id','=','games.id')
- > where('userGames.user_id','=','1') - > ;取(3) - >跳过(2) - >得到();

通过收听 illuminate.query 事件我可以看到,由此生成的查询是:

  select * from`games` 
inner join`userGames`
on`userGames`.`game_id` =`games`.`id`
其中`userGames`.`user_id` =?
limit 3 offset 2



雄辩ORM

限制

当我尝试用Eloquent重新创建相同的查询时:

  $ test = User :: find(1 ) - >游戏 - >取(2) - >指定者(); 

我可以使用 take 添加 skip 会导致错误。此外,结果查询实际上不包含限制:

  select`games`。*,`userGames`.`user_id` as `pivot_user_id`,
`userGames`.`game_id```````````````````````````````````````````````` game_id`
其中`userGames`.`user_id` =?

所以看起来整个结果是首先被查询,这在处理大数据时并不理想






问题:



是否可以限制一个雄辩的ORM查询,以便在MYSQL Query级别也限制结果,相当于 limit 3 offset 2

解决方案

  User :: find(1) - > games() - > take(3) - > skip(2) ;得到(); 

我认为这应该给你你的收藏。 :)



- >游戏将给您一个集合,其中 - >



享受Laravel!


TL;DR

Can you limit an Eloquent ORM query like using take() and skip() so that the resulting mysql query is also limited, and it doesn't have to return the entire dataset?

If so, how would you modify:

$test = User::find(1)->games->toArray();

To include limit 3 offset 2?


Tables:

users       games           userGames
-- id       -- id           -- user_id
-- name     -- name         -- game_id
            -- steam_id

Models:

class User extends Eloquent {
    public function games() {
        return $this->belongsToMany('Game', 'userGames', 'user_id', 'game_id');
    }
}

class Game extends Eloquent {
    public function users() {
        return $this->belongsToMany('User', 'userGames', 'user_id', 'game_id');
    }
}


Limit in Query Builder

Using the regular Laravel Query Builder I can get all games that belong to user of id 1, and limit the result with take() and skip():

$test = DB::table('games')
    ->join('userGames', 'userGames.game_id', '=', 'games.id')
    ->where('userGames.user_id', '=', '1')->take(3)->skip(2)->get();

By listening to the illuminate.query event I can see that the query generated by this is:

select * from `games`
inner join `userGames`
on `userGames`.`game_id` = `games`.`id`
where `userGames`.`user_id` = ?
limit 3 offset 2

Limit in Eloquent ORM

When I try to recreate the same query with Eloquent:

$test = User::find(1)->games->take(2)->toArray();

I'm able to use take but adding skip causes an error. Also the resulting query does not actually contain the limit:

select `games`.*, `userGames`.`user_id` as `pivot_user_id`,
`userGames`.`game_id` as `pivot_game_id` from `games`
inner join `userGames`
on `games`.`id` = `userGames`.`game_id`
where `userGames`.`user_id` = ?

So it seems that the entire result is being queried first, which is not ideal when dealing with large data sets.


Question:

Is it possible to limit an Eloquent ORM query so that at the MYSQL Query level it also limits the result, equivalent to limit 3 offset 2?

解决方案

User::find(1)->games()->take(3)->skip(2)->get();

I think this should give you your collection. :)

->games will give you a collection, where ->games() will offer a query builder instance.

Enjoy Laravel!

这篇关于laravel 4 - 如何限制(采取和跳过)雄辩的ORM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆