In Eloquent 5.2.7 I was able to add to the result set using
foreach($page as $xp => $xv) {
$content[0]->{$xp} = $xv;
}
Where $content was a DB::table()->get() result.
Now, on the latest version I get the following errors:
Indirect modification of overloaded element of Illuminate\Support\Collection has no effect in
and
Undefined offset: 0 in /vendor/illuminate/support/Collection.php
I think I understand the why, and it's to do with PHP and ArrayAccess and offsetGet but I cannot figure out how to effectively do what I am doing "the right way"
You can also use the collection map() method to loop through and modify the results:
DB::table()->get()->map(function($content) use ($page) {
foreach($page as $xp => $xv) {
$content->{$xp} = $xv;
}
return $content;
});
After some more debugging, looks like I can manipulate the data like that, I just cannot pass it empty data - will need to check for empty arrays.
Also in order to display I needed to add $content->all().
Related
I'm trying to get all the collection from a database in mongodb with PHP but I didn't find anything on internet useful.
This is what I get so far
$client = new MongoDB\client;
$database = $client->database2;
$collections = $database->listCollections();
foreach ($collections as $collection) {
echo $collection;
}
I'm using listCollections() but doesn't work. I just need to get all data to show it.
This is my folder structure if it helps
This is the output:
Recoverable fatal error: Object of class MongoDB\Model\CollectionInfo could not be converted to string
Finally, I could do it with getName() to get all the collections name from my database.
$colecciones = $database->listCollections();
foreach ($colecciones as $col) {
echo $col->getName();
}
Your result is an object and you try to convert them to a string with an echo. So use var_dump() for example to make the object visible. Then you can use the methods to get more informations from the collection.
One of the methods is getName() you can use them to get the name of the collection.
MongoDBModelCollectionInfo
So i have a problem when i want to add element to Paginator collection, i got this error :
call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Support\Collection' does not have a method 'add'
This is my code :
$data = $users->whereBetween('program_date',[$from,$to . ' 23:59:59'])->paginate(10);
$tests = $test->whereBetween('created_at',[$from,$to . ' 23:59:59'])->paginate(10);
foreach ($tests as $sms) {
$camp = new Campagne();
$camp->user_id = $sms->user_id;
$data->add($camp); // i got error in this line!!
}
so please if someone has any idea how to use ->add($element) with laravel pagination i will be very appreciative.
I don't think Collection has a function called add. also I am not sure you can loop through, at least not the models, with straight from Paginator. I believe you need to change it to
foreach( $tests->getItems() as $sms ){
.....
}
the reason it won't work this way is when you paginate a collection you get an instance of Illuminate\Pagination\Paginator class and when you loop through, you are basically looping class object not your items.
Zend framework 2 newb here so bear with me. See the code below:
public function setMediaToTrue($users) {
foreach($users as $user) {
$user->media = true;
}
return $users;
}
This method receive $users ResultSet object and update the media property to true. The problem is I can't modify the property of $users object inside iteration. Things that I have tried :
1. Use $users->current() inside iteration. example below :
$users->current()->media = true;
Create setMedia inside $user model and use it inside iteration:
$user->setMedia(true); // This does not update the property either
I think the problem because ZF2 create new copy of object for each iteration. I was looking at using Hydrator but can't get my head around it.
Thanks
I have the following piece of code, which generates six drop-down elements:
for($i = 0; $i < 6; $i++)
{
$RelatedProductsHtmlList .= $this->getRelatedProductHtmlDropdown($Products[$i], $allAvailibleProducts, $i);
}
In this code, the argument $Products[$i] is passed, which is a ORM object with information for setting the default selected value of the drop-down list that is generated. I have the problem that $Products is not always an array. Sometimes only contains one value, in which case it's not an array, but a single ORM object.
What is the cleanest thing to do? Convert $Products to an array with only one element? Always pass the entire $Products variable en determine in the function if it's an array? Or determine if $Products is an array before calling the function and set the function argument accordingly?
You have two options: fix it before calling the method or inside the method itself.
Example:
if(!is_array($products)) {
$products = array($product));
}
If you ask me, I would add this code to the top of the method itself as this would ease the function call and reduce redundant code.
I would suggest to allow to pass array and a single object into a function. You will avoid multiple checks in different parts of code. You can do it this way:
/**
#param array | ProductClass $Products
...
*/
public function getRelatedProductHtmlDropdown($Products, $allAvailibleProducts, $i)
{
if (!is_array($Products)) $Products = array($Products);
....
}
For a php project I use a Collection class to handle my objects and lazyloading in Java-like collections.
Now my object has a collection of emailaddresses for example. So I call the getEmailAddresses() function of the object which calls the mapper to return a collection of emailaddresses.
This works fine, but when I do a foreach loop over my collection it returns valid data with the following error in the end:
Fatal error: Call to a member function create() on a non-object in /foo/bar/Collection.php on line 89
It directs to the following function:
public function current()
{
if ($this->_collection instanceof Iterator)
$key = $this->_collection->key();
else
$key = key($this->_collection);
if ($key === null)
return false;
$item = $this->_collection[$key];
if (!is_object($item)) {
$item = $this->_gateway->create($item);
$this->_collection[$key] = $item;
}
return $item;
}
This line:
$item = $this->_gateway->create($item);
The _gateway is the adapter the collection uses. Which I don't use and keep it null. Maybe it has something to do with that?
Anybody has some clue? Because everything is functioning as it should, I can read the collectiondata. It's just the error.
Replace
if (!is_object($item))
with
if (!is_object($item) && !is_null($this->_gateway))
This of course only makes sure the code doesn't get called if gateway isn't set, so it doesn't do anything to $item (which might not be what you want).
Already got it!
It appears that if the collection requested doesnt have any objects, it just tries to do something.
If I first count the items and compare it to > 0 it doesnt return any errors. This is going to be an issue so I'll update the class to check for it first.
I'm not the only one working with it, this is not an error you would expect.
This only means that $this->_gateway is not an object and it should be. It can't be null.
You can change this line:
$item = $this->_gateway->create($item);
to
if(is_object($this->_gateway)) {
$item = $this->_gateway->create($item);
}
this will fix this error, but can lead to more errors further down, depending on what exactly is $item supposed to be.