2016-02-26 81 views
2

我想从Eloquent(Laravel 4.2)得到结果到简单的数组中,所以我决定让array_diffEloquent(Laravel)结果到数组

在文档中,我发现all()函数,它给出了数组的结果,但也是我不能拥有的东西,因为array_diff

我:

$curCentres = Doccentre::where('r_document', $document)->select('r_id')->get()->all(); 

但是这回是这样的:

array(2) { 
    [0]=> object(Doccentre)#1125 (20) { 
     ["table":protected]=> string(9) 
      "doccentre" ["primaryKey":protected]=> string(4) 
      "r_id" ["timestamps"]=> bool(false) 
      ["connection":protected]=> NULL 
      ["perPage":protected]=> int(15) 
      ["incrementing"]=> bool(true) 
      ["attributes":protected]=> array(1) { 
       ["r_id"]=> string(1) "1" 
      } 
      ["original":protected]=> array(1) { 
       ["r_id"]=> string(1) "1" 
      } 
      ["relations":protected]=> array(0) { } 
      ["hidden":protected]=> array(0) { } 
      ["visible":protected]=> array(0) { } 
      ["appends":protected]=> array(0) { } 
      ["fillable":protected]=> array(0) { } 
      ["guarded":protected]=> array(1) { 
       [0]=> string(1) "*" 
      } 
      ["dates":protected]=> array(0) { } 
      ["touches":protected]=> array(0) { } 
      ["observables":protected]=> array(0) { } 
      ["with":protected]=> array(0) { } 
      ["morphClass":protected]=> NULL 
      ["exists"]=> bool(true) 
     } 
    [1]=> object(Doccentre)#1124 (20) { 
     ["table":protected]=> string(9) 
     "doccentre" ["primaryKey":protected]=> string(4) 
     .... 
} 

所有我需要的是:

array(2) { [0]=> string(1) "1" [1]=> string(1) "2" } 

有什么办法得到它?我也试过toArray(),但它只会产生错误。

+0

你可以使用:https://www.neontsunami.com/posts/diffing-eloquent-collections-in-laravel-4这很酷,因为你仍然可以拥有你的集合(这是在类固醇的**数组* *)。 –

回答

4

你可以使用lists来检索列值的列表:

$curCentres = Doccentre::where('r_document', $document)->lists('r_id'); 

希望这有助于。

+1

谢谢,它有帮助。但没有toArray(),因为列表已经返回数组。 –