获得对象laravel 4的价值

时间:2014-11-30 01:58:26

标签: php arrays object laravel laravel-4

我得到以下对象数据

  ["id"]=> string(1) "1" 
  ["created_at"]=> string(19) "0000-00-00 00:00:00" 
  ["updated_at"]=> string(19) "0000-00-00 00:00:00" 
  ["wording"]=> string(1174) "Some Text"  
  ["container"]=> string(8) "training" 

  ["id"]=> string(1) "2" 
  ["created_at"]=> string(19) "0000-00-00 00:00:00" 
  ["updated_at"]=> string(19) "0000-00-00 00:00:00" 
  ["wording"]=> string(1174) "Some Text"  
  ["container"]=> string(8) "relax" 

我使用

从数据库中获取
 $wording = text_start::all();

现在,我想找到container = training, container = relax, ...的位置并相应地回应相应的措辞:

$training = $wording->contains('training');
$taste = $wording->contains('taste');
$relax = $wording->contains('relax');
$risultati = $wording->contains('risultati');


<span><h2>{{ Str::upper($wording[$relax]->container) }}</h2></span>
<span>{{ Str::limit($wording[$relax]->wording ,400,'...') }}</span>

不幸的是,我每次都得到培训的内容。我怎么能这样做?

2 个答案:

答案 0 :(得分:3)

包含功能并不意味着以您使用它的方式使用

contains返回文档中指定的布尔值。

http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Collection.html#method_contains

所以每次如果它包含那个键(它不是因为训练不是键),它就会返回true。因此,每次函数返回false0时。然后它会选择正在训练的0th索引

如果我理解你正确,你试图像where一样使用它。我建议先排序你先获得的数组,然后选择合适的订单

usort($wording, function($a, $b) {
      return $a['training'] - $b['training'];
});

<span><h2>{{ Str::upper($wording[$relax]->container) }}</h2></span>
<span>{{ Str::limit($wording[$relax]->wording ,400,'...') }}</span>

或者,您可以重新构建数据本身。

答案 1 :(得分:2)

使用集合的keyBy方法:

$wording = text_start::all()->keyBy('container');
<span><h2>{{ Str::upper($wording['relax']->container) }}</h2></span>
<span>{{ Str::limit($wording['relax']->wording ,400,'...') }}</span>