I get the following object data
["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"
Which I get from the DB using
$wording = text_start::all();
Now, I want to find where container = training, container = relax, ...
and echo the appropriate wording accordingly:
$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>
Unfortunatelly, I get the contents of training every time. How could I do this?
Use the collection's keyBy
method:
$wording = text_start::all()->keyBy('container');
<span><h2>{{ Str::upper($wording['relax']->container) }}</h2></span>
<span>{{ Str::limit($wording['relax']->wording ,400,'...') }}</span>
The contains function is not meant to be used the way you are using it
contains
returns a boolean value that is specified in the docs.
http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Collection.html#method_contains
So every time if it contains that key (which it doesn't as training is not a key), it returns true. Hence every time the function returns false
or 0
. It then picks up the 0th
index which is training
If I understand you correctly you are attempting to use it like a where
.I would recommend sorting the array you get first and then picking up the appropriate order
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>
Alternately you could restructure the data itself.