Laravel in controller return new collection of results from a foreach loop through records - eloquent

This seems like it should be obvious, but everything I find relates to returning a collection extracted from records, rather then returning a collection of new results derived from calculations on the records.
For instance, say I have records of property in my database. I can extract a collection of a subset (or the entire set) of the records. But I want to loop through this collection, calculate new values for each line item, (like marketValue-debt=netValue) and return a new collection of just those results to my view. I'm trying to keep my (much more complicated than this example) calculation in my controller and out of my view, but I'm not getting the way to stuff new values into a new collection of results for return to display in the view.
I could derive my results and stuff them into an array, but how do I pass this as a new collection for looping through in my view to show those results? Seems like there should be an Eloquent way to do this.
My project is in Laravel 6 running on Apache/Laragon, PHP 7 with MariaDb
Thanks in advance for helping me out.

Take a look at this. Hope it will be of some help.
If you want a new collection, use the map method.
$new_collection = $old_collection->map(function ($item) {
return $item->marketValue - $item->debtValue;
});

Related

Build couchbdb view to index all documents whose ID starts with various three or four characters?

I'm new to nosql and views. Wondering if someone could show me how to build an index such that it will return all the different documents that apply multiple different keys. An example is below.
I have many many documents that all have the naming convention as follows:
AABA_August-11-2017_2017-06-29_10
BBY_August-11-2017_2017-06-29_10
CECO_January-19-2018_2017-06-08_19
GEL_December-15-2017_2017-06-08_1
Etc..
I'd like a view such that I could query on "starts with BBY" for example. And it would return all the documents that start with BBY. Maybe even "BBY_December", "BBY_August" etc.
Wondering if this is possible and what it would look like. I'm using CouchDB which uses Mango to build indexes. If someone could just point me in the right direction that would help too.
Thanks
You could write such a view like this:
function(doc) {
var docId = doc._id;
var p = docId.substring(0, 2); // Or however many chars you want
if (p === 'BBY') emit(doc._id, doc); // Or whatever kind of key you want
}
And then write similar views for alternate prefixes. You can also use query parameters similar to the _all_docs endpoint with views (http://docs.couchdb.org/en/2.0.0/api/ddoc/views.html).
I think the only benefit of using a view instead of what you have done is that you can filter unnecessary fields, do some basic transformations, etc.
Considering the similarities between retrieval from _all_docs vs from a view, it looks like the _all_docs endpoint is just index similar to a custom view. But I'm not sure on that.
Not sure how to use Mango to do the same.
My current naming convention required no new index. I used futon to find:
ip:port/DB/_all_docs?inclusive_end=true&start_key="BBY_Aug"&end_key="BBY_Auh"

loading dataset where the dataadapter select command uses the IN clause with a subquery

I'm loading a subset of the parent records and the child records into a dataset then setting datarelations and foreignkeyconstraints, so when I am building the dataadapter select stmt for the child records, I must make sure that only the child records whose parent is present is loaded to avoid referential integrity errors. Since the subset of the ParentTable has been loaded into a dataset I tried:
daChild = new OleDBDataAdapter(CreateOledbCommand("select * from Childtable where ChildKey in (ds.tables(""ParentTable""))",dconn))
But got an "undefined function 'ds.tables' in expression" error at runtime.
So, I tried to create a one column table to pass to the IN clause, like this:
Dim MyDataView as DataView = New DataView(ds.tables("ParentTable"))
Dim MyTempParentTable as DataTable = MyDataView.ToTable(False, "ParentKey")
daChild = New OleDbDataAdapter(CreateOledbCommand("select * from ChildTable where ChildKey in (MyTempParentTable)", dconn))
I checked in the debugger, and MyTempParentTable is, in fact, a one column table containing the key of the ParentTable. I thought that the IN clause could take a one column datatable as valid input. Apparently not, as I am getting a "No value given for one or more required parameters" error at runtime.
I'm just about out of ideas. All my google searching came up with for the IN clause was something like IN (value1, value2, value3...) and doesn't give any examples for the IN clause containing a more complex expression.
Can anyone clue me in on what I'm missing, or maybe a different way to accomplish this? I'm trying to get the IN clause to work because I'm thinking that it is the most efficient way to do this... Maybe it's not... Thanks for any help.
I haven't found an answer to the question about why the IN clause didn't work, but just in case anybody else has this issue I will post my workaround.
I load the parent records as usual, but my dataadapter select stmt for the child records has a "WHERE 1=0" so that zero records are initially loaded. When a new parent record is displayed I then determine if the child records for that parent are already loaded into the dataset, if not, I load them in. Bottom line is that I don't pre-load the child records, rather, I load them "on demand". This solution is working well for me.

Meteor/Mongo - Ensuring that one collection gets filled before another

Is there a way to ensure that one script runs before another in Meteor? I'm currently developing some software and using sample data for now. I'm a bit curious if there's a way that I can fill a particular collection only after another collection that it depends on has been filled
For example, An Invoices collection that has a patient_id: Patients.findOne(...) field that depends on the Patients collection actually having data. Is there a way to perform this other than having them on the same file, with Patients being filled before Invoices?
Assuming you are trying to create test data in the right order, then you can run the test data generator for Invoices in a Tracker.autorun. This will be run reactively:
Meteor.startup(()=>{
Tracker.autorun(()=>{
if ( Patients.find().count() && !Invoices.find().count() ){
populateInvoices();
}
});
});

Finding the number of different values of an attribute

I am writing an APS.NET MVC 5 application in C#, using a MongoDB database. Suppose I have a MongoDatabase object called my_db, which contains a MongoCollection of Label objects, called labels. Each Label object has a few attributes, one of which is a string called tag. Each tag value may be shared across different Labels, such that some Label objects will have the same value for tag.
I want to find out how many different values for tag there are in this collection, and store these values in an array of some sort.
I'm fairly new to MongoDB, so I don't really know how to do this. All I have done so far is get labels:
var labels = my_db.GetCollection<Label>("labels");
But I'm stuck as to what I need to do now. I could manually iterate through each Label in labels, and check whether that Label's tag attribute has already been seen before. But is there a neater way to do this with a MongoDB function? Thanks!
There is a MongoDB method for this: distinct, that should exist in any API.
As you are doing this on MVC 5 c# application, MongoDB provides C# LINQ Driver which will help your querying MongoDB using LINQ.
http://docs.mongodb.org/ecosystem/tutorial/use-linq-queries-with-csharp-driver/
Hope this helps.
var query = (from e in labels.AsQueryable<labelClass>()
select e.tag).Distinct()

Zend Paginator Filter results

I have a results set using Zend Paginator, its all working fine but I need to filter the results by using checkbox options like on eBay, Amazon etc..
I have read about Zend filter but dont know where to start.
Do anyone have any experience of this please?
Thanks
John
Zend_Filter is to transform something into something, like a translator.
The huge advantage of Zend_Paginator is that it retrieves data page by page. Figure out you want to display 30 items from a several millions table. It would be a very slow process if you retrieve all data and then filter accordingly to take out those 30 items.
Supposing you are retrieving your items from the database, your should use a DbSelect or DbTableSelect adaptor. Use DbTableSelect if you retrieve from one table and DbSelect if you retrieve from more than one table by joining.
Once you have your $select object with which you would retrieve all items in all pages, the paginator will retrieve the items page by page by setting a limit and offset. For instance, if you are displaying 30 items per page and you want to display the page number 3, the paginator will set $select->limit(30, 60) (you don't need to do this, the paginator does it for you). To accomplish this, try following in your controller:
// pass ?page=N in the url
$pageNumber = $this->getRequest()->getParam('page', 1);
// itemsPerPage can also be read from the url
$itemsPerPage = 30;
// this $select must retrieve all items in all pages
$select = new Zend_Db_Select();
$select->from(...)
// eventually joins, where, or order statements
$paginator = new Zend_Paginator($select);
$paginator->setCurrentPageNumber($pageNumber);
$paginator->setItemCountPerPage($itemsPerPage);
You finally need to pass the paginator to the template is rendering the items and the pagination control. So add
$this->view->paginator = $paginator;
and edit the template as in http://framework.zend.com/manual/1.12/en/zend.paginator.usage.html#zend.paginator.rendering
So it's the basic functionality of the paginator. I hope this helps. It would be very similar if you are using other type of adapters like Array or Iterator.
The checkbox options have nothing to do with Zend_Paginator.
Zend_Paginator is just a Helper class that helps you paginate your results, it adds LIMIT clause and make templating the results easier, nothing more.
What you're lookig for is called 'Faceted search', you have to develop the logic behind it or use a search engine like Solr that can generate it automatically