I'm currently doing this:
$t_category = 781;
$res = $collection->findOne(array("_id" => intval($t_category)));
this way i am getting only one category (it was just to try). How can I get all categories (documents->_ids) in php->mongo? A way I could iterate over all the documents in the collection. I have tried the find with no arguments. No luck. Any idea?
Calling find with no arguments will return all items in the collection.
$results = $collection->find();
Related
I am trying to display buildings and circuits in list (only 3 in total, sorted by the column "updated_at"). The problem is that they are not in the same table. I have a table circuits and a table buildings.
I tried to do this:
$buildings = Building::published()->limit(3)->get();
$circuits = Circuit::published()->limit(3)->get();
$merged = $buildings->merge($circuits);
$this->data['buildingsAndCircuits'] = $merged->all();
I get everything right when I'm doing my var_dump and I know how to access the data when I do a foreach. But that does not do what I want.
I would like to sort them (by updated_at) and have only three and not six.
Is there a way to do that?
I tried to make conditions like with the QueryBuilder on $merge but it does not work
$this->data['buildingsAndCircuits'] = $merged->orderBy('updated_at', 'DESC')->limit(3)->all();
thank you very much
Once you've called get on each query then the query is executed and the result is returned. orderBy will not longer work after that since what you have in $buildings, $circuits and $merged is a collection.
You can however do this:
$buildings = Building::published()->limit(3)->get();
$circuits = Circuit::published()->limit(3)->get();
$merged = $buildings->merge($circuits)->sortByDesc('updated_at');
$this->data['buildingsAndCircuits'] = $merged->all();
Check what else you can do on collections in the documentation
I don't get this, I have a collection of items.
The collection contains in this case one and only one item at index 1 as it was filtered from a bigger collection.
The point is how do I get the only piece of data I need without having to reset the values of the index and then accessing at index 0 ??
This is a case where I will always have only ONE item in the collection but the index at could be different so I can't use the [0] index by default.
//returns all items with 'item_color_id" = 1
$item = Item::where(//some constraints);
$filtered = $item->filter(function ($i) {
return $i->item_color_id == 1;
});
if (count($filtered)) {
//need to access a single data inside the collection
// like for example 'item_brand_id'
//I can do it like this:
$filtered = $filtered->values();
$item_brand_id = $filtered[0]['item_brand_id'];
//but what sense does it have?? how can we access 'item_brand_id'
//from $filtered without resetting the indexes?
}
it doesn't make any sense to me that we don't have a method to access the data directly, or if we have it I missed it.
For example I coould use max() or min() on $filtered like this:
$max_brand_id = $filtered->max('item_brand_id');
It doesn't make any sense to find the max id in this case I know, but it shows that we can find the data in one passage.
I tried only('item_brand_id); but that returns empty while the data is there.
You still have eloquent collection so you can do just call first() function
$item_brand_id = $filtered->first()->item_brand_id;
filter() is used when you want a collection of matching elements, even if that only results in one element.
If you know that you only want one element that matches a truth test, use first() instead. It has the same signature.
Item::where(...)->get()->first(function ($item) {
return $item->item_color_id == 1;
})->get('item_brand_id');
You could change your database query to just return one element. If the result is always just one, there is no need to load an entire collection.
$item = Item::where(/*some constraints*/)->where('item_color_id', 1)->first();
if (isset($item))
echo $item->item_brand_id;
I have a questions table and a tags table. I want to fetch all questions from tags of a given question. So, for example, I may have the tags "Travel," "Trains" and "Culture" attached to a given question. I want to be able to fetch all questions for those three tags. The tricky, so it seems, is that questions and tags relationship is a many-to-many defined in Eloquent as belongsToMany.
I thought about trying to merge the questions Collections as below:
foreach ($question->tags as $tag) {
if (!isset($related)) {
$related = $tag->questions;
} else {
$related->merge($tag->questions);
}
}
It doesn't seem to work though. Doesn't seem to merge anything. Am I attempting this correctly? Also, is there perhaps a better way to fetch a row of rows in a many-to-many relationship in Eloquent?
The merge method returns the merged collection, it doesn't mutate the original collection, thus you need to do the following
$original = new Collection(['foo']);
$latest = new Collection(['bar']);
$merged = $original->merge($latest); // Contains foo and bar.
Applying the example to your code
$related = new Collection();
foreach ($question->tags as $tag)
{
$related = $related->merge($tag->questions);
}
The merge() method on the Collection does not modify the collection on which it was called. It returns a new collection with the new data merged in. You would need:
$related = $related->merge($tag->questions);
However, I think you're tackling the problem from the wrong angle.
Since you're looking for questions that meet a certain criteria, it would probably be easier to query in that manner. The has() and whereHas() methods are used to generate a query based on the existence of a related record.
If you were just looking for questions that have any tag, you would use the has() method. Since you're looking for questions with a specific tag, you would use the whereHas() to add the condition.
So, if you want all the questions that have at least one tag with either 'Travel', 'Trains', or 'Culture', your query would look like:
$questions = Question::whereHas('tags', function($q) {
$q->whereIn('name', ['Travel', 'Trains', 'Culture']);
})->get();
If you wanted all questions that had all three of those tags, your query would look like:
$questions = Question::whereHas('tags', function($q) {
$q->where('name', 'Travel');
})->whereHas('tags', function($q) {
$q->where('name', 'Trains');
})->whereHas('tags', function($q) {
$q->where('name', 'Culture');
})->get();
$users = User::all();
$associates = Associate::all();
$userAndAssociate = $users->merge($associates);
Merge two different eloquent collections into one and some objects happen to have the same id, one will overwrite the other. Use push() method instead or rethink your approach to the problem to avoid that.
Refer to web
Creating a new base collection for each eloquent collection the merge works for me.
$foo = collect(Foo::all());
$bar = collect(Bar::all());
$merged = $foo->merge($bar);
In this case don't have conflits by its primary keys.
I have faced some issue by using merge. So I used concat. You can used it like below.
$users = User::all();
$associates = Associate::all();
$userAndAssociate = $users->concat($associates);
All do not work for me on eloquent collections, laravel eloquent collections use the key from the items I think which causes merging issues, you need to get the first collection back as an array, put that into a fresh collection and then push the others into the new collection;
public function getFixturesAttribute()
{
$fixtures = collect( $this->homeFixtures->all() );
$this->awayFixtures->each( function( $fixture ) use ( $fixtures ) {
$fixtures->push( $fixture );
});
return $fixtures;
}
I'm sorry about that, but since PHP 7.4 you're available to do like this (better use merge).
$foo = Foo::all();
$bar = Bar::all();
/** $foo will contain $foo + $bar */
$foo->push(...$bar);
I would like to add that, i found that the concat method does not seem to override based on ID, while the merge method does. concat seems to work for me, while merge caused issues.
I'm having issues with an array returned from DB::select(). I'm heavily using skip and take on Collections of eloquent models in my API. Unfortunately, DB::select returns an array, which obviously doesn't work with skip and take's. How would one convert arrays to a collection that can utilise these methods?
I've tried
\Illuminate\Support\Collection::make(DB::select(...));
Which doesn't quite work as I expected, as it wraps the entire array in a Collection, not the individual results.
Is it possible to convert the return from a DB::select to a 'proper' Collection that can use skip and take methods?
Update
I've also tried:
$query = \Illuminate\Support\Collection::make(DB::table('survey_responses')->join('people', 'people.id',
'=', 'survey_responses.recipient_id')->select('survey_responses.id', 'survey_responses.response',
'survey_responses.score', 'people.name', 'people.email')->get());
Which still tells me:
FatalErrorException in QueryHelper.php line 36:
Call to a member function skip() on array
Cheers
I would try:
$queryResult = DB::table('...')->get();
$collection = collect($queryResult);
If the query result is an array, the collection is filled up with your results. See the official documentation for the collection. Laravel5 Collections
For anyone else that's having this sort of problem in Laravel, I figured out a work around with the following solution:
$query = DB::table('survey_responses')->join('people', 'people.id', '=', 'survey_responses.recipient_id')
->select('survey_responses.id', 'survey_responses.response', 'survey_responses.score', 'people.name', 'people.email');
if(isset($tags)){
foreach($tags as $tag){
$query->orWhere('survey_responses.response', 'like', '%'.$tag.'%');
}
};
// We apply the pagination headers on the complete result set - before any limiting
$headers = \HeaderHelper::generatePaginationHeader($page, $query, 'response', $limit, $tags);
// Now limit and create 'pages' based on passed params
$query->offset(
(isset($page) ? $page - 1 * (isset($limit) ? $limit : env('RESULTS_PER_PAGE', 30)) : 1)
)
->take(
(isset($limit) ? $limit : env('RESULTS_PER_PAGE', 30))
);
Basically, I wasn't aware that you could run the queries almost incrementally, which enabled me to generate pagination chunks before limiting the data returned.
I have an array of Ids which has already been ordered based on some criterias
Example:
$array = array(5,4,10,3,1);
and i am using yii to get all these records from database:
$records = TableName::model()->findAllByPk($array);
this is returning me the correct results but its being sorted by the primary key in ascending order.
i even tried using CDbCriteria like this:
$criteria = new CDbCriteria();
$criteria->addInCondition('primaryKeyColumnName',$array);
$records = TableName::model()->findAll($criteria);
and its still being sorted.. can anyone help me plz
Add This line and try I think It will work as you want.
$criteria->order = "FIELD(id, '5,4,10,3,1')";