如何限制Laravel's Eloquent的结果数据?

时间:2014-12-29 19:55:28

标签: php sql laravel laravel-4 eloquent

我是Laravel的新手,我想知道我是应该使用Eloquent Query Builder还是仅使用原始SQL。 当我使用Eloquent从MySql中提取数据时,我得到了许多无关紧要的东西。

我使用Eloquent Query Builder的方法:

$query = Document::where('owner', $user_id);
$docs = $query->take(2)->get();
return $docs;

我得到的结果:

object(Illuminate\Database\Eloquent\Collection)[191]
protected 'items' => 
array (size=2)
  0 => 
    object(Document)[189]
      protected 'table' => string 'documents' (length=9)
      protected 'connection' => null
      protected 'primaryKey' => string 'id' (length=2)
      protected 'perPage' => int 15
      public 'incrementing' => boolean true
      public 'timestamps' => boolean true
      protected 'attributes' => 
        array (size=12)
          'id' => int 2
          'title' => string 'test' (length=4)
          'description' => null
          'status' => int 0
          'files' => null
          'uid' => string 'gregdf' (length=6)
          'path' => string '[]' (length=2)
          'owner' => int 1
          'workspace' => int 1
          'flow' => int 1
          'created_at' => string '2014-12-10 10:10:10' (length=19)
          'updated_at' => string '0000-00-00 00:00:00' (length=19)
      protected 'original' => 
        array (size=12)
          'id' => int 2
          'title' => string 'test' (length=4)
          'description' => null
          'status' => int 0
          'files' => null
          'uid' => string 'gregdf' (length=6)
          'path' => string '[]' (length=2)
          'owner' => int 1
          'workspace' => int 1
          'flow' => int 1
          'created_at' => string '2014-12-10 10:10:10' (length=19)
          'updated_at' => string '0000-00-00 00:00:00' (length=19)
      protected 'relations' => 
        array (size=0)
          empty
      protected 'hidden' => 
        array (size=0)
          empty
      protected 'visible' => 
        array (size=0)
          empty
      protected 'appends' => 
        array (size=0)
          empty
      protected 'fillable' => 
        array (size=0)
          empty
      protected 'guarded' => 
        array (size=1)
          0 => string '*' (length=1)
      protected 'dates' => 
        array (size=0)
          empty
      protected 'touches' => 
        array (size=0)
          empty
      protected 'observables' => 
        array (size=0)
          empty
      protected 'with' => 
        array (size=0)
          empty
      protected 'morphClass' => null
      public 'exists' => boolean true
  1 => 
    object(Document)[187]
      protected 'table' => string 'documents' (length=9)
      protected 'connection' => null
      protected 'primaryKey' => string 'id' (length=2)
      protected 'perPage' => int 15
      public 'incrementing' => boolean true
      public 'timestamps' => boolean true
      protected 'attributes' => 
        array (size=12)
          'id' => int 3
          'title' => string 'test' (length=4)
          'description' => null
          'status' => int 0
          'files' => null
          'uid' => string 'gregdf' (length=6)
          'path' => null
          'owner' => int 1
          'workspace' => int 1
          'flow' => int 1
          'created_at' => string '0000-00-00 00:00:00' (length=19)
          'updated_at' => string '0000-00-00 00:00:00' (length=19)
      protected 'original' => 
        array (size=12)
          'id' => int 3
          'title' => string 'test' (length=4)
          'description' => null
          'status' => int 0
          'files' => null
          'uid' => string 'gregdf' (length=6)
          'path' => null
          'owner' => int 1
          'workspace' => int 1
          'flow' => int 1
          'created_at' => string '0000-00-00 00:00:00' (length=19)
          'updated_at' => string '0000-00-00 00:00:00' (length=19)
      protected 'relations' => 
        array (size=0)
          empty
      protected 'hidden' => 
        array (size=0)
          empty
      protected 'visible' => 
        array (size=0)
          empty
      protected 'appends' => 
        array (size=0)
          empty
      protected 'fillable' => 
        array (size=0)
          empty
      protected 'guarded' => 
        array (size=1)
          0 => string '*' (length=1)
      protected 'dates' => 
        array (size=0)
          empty
      protected 'touches' => 
        array (size=0)
          empty
      protected 'observables' => 
        array (size=0)
          empty
      protected 'with' => 
        array (size=0)
          empty
      protected 'morphClass' => null
      public 'exists' => boolean true

我使用SQL的方法:

$sqlArgs = array(
        'workspace' => $args['workspace'],
        'usr' => $user_id
    );

 $query = 'select d.title, d.description, d.created_at
                      from documents as d
                      inner join userDocuments as ud on ud.document = d.id
                      where ud.user = :usr and d.workspace = :workspace  limit 0, 2 ';
 $docs = DB::select($query, $sqlArgs);

 return $docs;

和结果(这是我真正需要的):

array (size=2)
0 => 
  object(stdClass)[185]
    public 'title' => string 'test' (length=4)
    public 'description' => null
    public 'created_at' => string '2014-12-10 10:10:10' (length=19)
1 => 
  object(stdClass)[189]
    public 'title' => string 'test' (length=4)
    public 'description' => null
    public 'created_at' => string '0000-00-00 00:00:00' (length=19)

那么哪种方法更好?或者我可以以某种方式限制Eloquent方法中的数据量?

更新:以上两个示例都在查询不同的记录集,但重要的是每个记录有多少数据是返回。

3 个答案:

答案 0 :(得分:0)

您可以指定您不感兴趣的字段。

class Car extends Eloquent {

    protected $hidden = array('created_at');

}

答案 1 :(得分:0)

您需要将Select用于您想要的列,如下所示

$query = Document::where('owner', '=', $user_id)->lists('title', 'description', 'created_at');

它只会有titledescriptioncreated_at列。 为了限制雄辩查询,您可以将takeselect一起使用,如下所示

$query = Document::where('owner', '=', $user_id)->lists('title', 'description', 'created_at')->take(5)->get();

答案 2 :(得分:0)

更多的是设计问题。 Eloquent是一个ORM(彻底阅读有关ORM的内容),即它通过对象表示所有记录,所有记录及其相关记录可以以更面向对象的方式进行操作。您可以查询数据库和/或在PHP中创建其架构,而不是触摸SQL。这样,可以在DB上放置过多的抽象。

为了提供思考的食物,人们可以通过雄辩,在多个部署中以不同方式操纵相同的数据库,为可能拥有自己的主键和/或关系的单个表设置截然不同的配置文件。

手工定制的SQL总是比使用任何ORM更快。这实际上取决于你认为更好的选择。

虽然toArray()方法更清晰,但我倾向于避免使用它。您可以按$fieldValues = $model->attributes提取单个模型的字段值。