Laravel relationship values execute in controller - laravel-5.1

I called a table with relationship in controller.
I want to print the relationship values in controller.
Here I have given below the sample:
TestItem::where('test_id', $testId)->with('testresults')->get();
How can I print the values of testresults in controller?

You create an object of result then print that object using php function like print_r(array) and exit flow using exit();
$TestItem = TestItem::where('test_id', $testId)->with('testresults')->get();
print_r($TestItem);
exit();

I want to print a relationship values.
For example with(testresults) is there, i want to print that values in controller.
Not the TestItem table values.

Related

Adding a Value to Collection Items

I want to load data from many files. Each file is named with a date and I need to inject this date to each of the fetched Entries of my file.
I know I could do this with an foreach - loop before inserting the data into the collection, but I think there should be a better solution.
Content of one file
[{"price":"95,34","isin":"FR0000120073"},{"price":"113,475","isin":"CA13645T1003"}]
The Code I use to move the data into a collection.
$collection= collect(json_decode(File::get($file)));
I tried for example the "map" method, however I don't know how to pass an additional variable to the anonymous function.
The content of my collection should look like this:
[{"price":"95,34","isin":"FR0000120073","date":"2016-06-23"},{"price":"113,475","isin":"CA13645T1003","date":"2016-06-23"}]
Is there any simple solution using the collections or do I have to use a foreach-loop?
May be this will help
$collection = collect(json_decode(File::get($file)));
$collection = $collection->each(function ($item, $key) {
//First iteration of $item will be {"price":"95,34","isin":"FR0000120073"}
$item->date = "2016-06-23"; //Insert key, value pair to the collection
});

Fitnesse-Fixture for method that return data types like data set

I am trying to test the .NET class using fitnesse. Is it possible with fitnesse to test a method that returns (not a single value either string or integer) a data set.
Yes. For example, you can return a json object and view the contents of the json object as a return value of a function.
|script |className |
| $variable= |functionName|
For a json object returned, you should be able to view its contents assigned to $variable.
The steps associated with other data structures are similar
A dataset return value is automatically handled with an Array fixture:
|SomeClass|
|SomeMethodThatReturnsDataSet|
|column1|column2|column3|
|a|b|c|
|d|e|f|
See http://fitsharp.github.io/Fit/FixtureWrapper.html for more examples.
You can return IEnumerable class and check the table.
See whole tutorial there: https://github.com/imanushin/NetRunner/wiki/Net-Runner-tutorial

Active record where query for value inside of an array

Question: Is it possible to build a class method scope that can query objects based on values inside an array in a table? If yes, how can I do this?
In my example, I have a “wells” table that has an array field called “well_tags”. I want to build a query that returns all objects that have a specified value (such as “ceramic”) in the wells_tags array. The basic query would be something like this:
#well = Well.all
#query = #well.where(“well_tags contains ceramic”)
And then the class method scope would look something like this, with the “well_tag_search” param passed in from the controller:
class Well < ActiveRecord::Base
def self.well_tag_filter(well_tag_search)
if well_tag_search.present?
where(“well_tags contains ceramic")
else
Well.all
end
end
I found another post that asks a similar question (see link below), but I cannot get the answer to work for me...the result is always 'nil' when I know there should be at least 1 object. I am a beginner using sqlite (for now) as my database and rails 4.0.
Active Record Query where value in array field
Thanks!
UPDATE: some progress
I figured out how to create an array of all the objects I want using the ‘select’ method. But I still need to return the results as an Active Record object so I create a class method scope.
#well = Well.select
{ |well| if well.well_tags.present?
then well.well_tags.include? ‘ceramic' end }
#well.class #=> array
Not sure where Show is coming from.
Can you try doing Well.all instead of Show.all?

How to pass a map from gsp to controller as part of params in Grails remoteFunction

I am trying to send map from gsp to controller but the map is considered as a string in the controller
<g:remoteFunction
action="updateCart"
params="{startDate:stDt,endDate:endDt,cartId:pkid,shoppingCart:'${shoppingCart}'}"
update="resourcesSelectedId"/>
Here the shoppingCart is a grails map variable I am trying to send
Edit:
there was a typo in the code I posted above. Missed starting "{" in the params
params="{startDate:stDt,endDate:endDt,cartId:pkid,shoppingCart:'${shoppingCart}'}"
Updated my question as per my comments below
In my case shoppingCart is an object and it has, lets say for example, items and quantity of each item. I have some rules to be applied based on the items selected and quantity and determine the price for each item and show it back to the user. I want to do this processing the controller. Whenever user updates the cart I need to re-calculate and show it back to user. Is there any other better approach you would suggest to do this instead of passing the objects back and forth
Anytime you're using the params attribute, it has to be in the format of a Map anyway, which means including the [ ]. This also means you can exclude the ${ } from any values because grails will parse all these as potential variables.
<g:remoteFunction
action="updateCart"
params="[startDate:stDt,endDate:endDt,cartId:pkid,shoppingCart:shoppingCart]"
update="resourcesSelectedId"/>
However, keep in mind that you can't send objects. I'm not sure what shoppingCart is in your example, but it would only be able to be a simple value that can be represented as a String. Possibly you would want shoppingCart.id? Otherwise, that should get you going in the right direction.
I could able to overcome this issue by following below steps
converted the shoppingcart object to json string
Pass the json as part of the params
parse the json string server-side & process
pass the updated object back to gsp

Grails, groovy: combine GORM dynamic methods - findAllBy and OrderBy

assuming the following example:
I have a User class and a Item class and a user can have many items
1) Is there a combined dynamic method to get all the items for a user and also sort them after a Property? I have a controller action that gets the items for a user and send them to the view, and the view will render them all with < g:each>. But I want to sort them by any property of Item without sorting the array after it is relived from GORM (using sort in controller or view).
So basically what I do now is items = Item.findAllByOwner(userInstance)
and then send the [items:items.sort{it.property}] to the view. I want to combine findAllBy with Item.listOrderByProperty()
2) Assuming that there is such a method as on 1) : I want to use it to escape the overhead of sorting the array after constructing it. Will such a method be more performant instead of making a sort{} on items ?
You can pass a map of options to findAllBy. Take a look at http://www.grails.org/doc/1.2.2/ref/Domain%20Classes/findAllBy.html .

Resources