Laravel collection->diff() not working when using select - laravel

I have table where I want to find difference in certain columns. I'm doing it with diff():
$inventoryitems1 = InventoryItem::where('inventory_id', $request->get('inventory1'))
->select('owner_id', 'location_id', 'asset_id')
->get();
$inventoryitems2 = InventoryItem::where('inventory_id', $request->get('inventory2'))
->select('owner_id', 'location_id', 'asset_id')
->get();
$difference = $inventoryitems2->diff($inventoryitems1);
And then i get empty array. But if I don't have select in my collection. It work, it shows all different rows. Is there anyother way to do this or this one is right but I'm not doing it the right way?
UPDATE
I have $inventoryitems1 collection that looks like this:
0: {asset_id: 2, owner_id: 2, location_id: 1}
1: {asset_id: 3, owner_id: 2, location_id: 1}
and $inventoryitems1 collection that looks like this:
0: {asset_id: 2, owner_id: 2, location_id: 1}
1: {asset_id: 3, owner_id: 6, location_id: 1}
I'm trying to find differences in those two collections, where we can see that owner_id is different in second collection. How can I only get those objects where there was some change (difference).

Related

How to return an array instead of a collection in Laravel?

In Laravel is it possible to select only one field and return that as a set/ array.
For example consider the model Foo which is linked to table foos which has field id, a, b, c.
Consider the following sample data:
(1, 10, 15, 20)
(1, 12, 15, 27)
(1, 17, 15, 27)
(1, 25, 16, 29)
(1, 28, 16, 40)
Now if I wanted to create a query that returns all the values of a where b is 15, I could do that like so:
Foo::select('a')->where('b', 15)->get();
However this will return an eloquent collection.
Instead how can I return instead an array like this:
[10, 12, 17]
Just use pluck() and ->toArray():
Foo::where('b', 15)->pluck('a')->toArray();
Laravel's pluck() method.
Laravel's toArray() method.
Do
Foo::where('b', 15)->lists('a')->all();
This will give you an array of ids. eg [2, 3, 5]
The lists method is deprecated and pluck need some parameter so, if you want to get all the attributed in array format, use it this way.
Foo::select('a')->where('b', 15)->get()->all();

Eloquent with(), select() and where() selection

This is my query to fetch data from 2 different table.
$variant = Variant::with(['v_detail' => function($q){
$q->select('variant_dtl_name');
}])->where('product_id','=',$productId)->get();
There is output, but v_detail returning empty list
result:
created_at: "2015-11-07 12:37:26"
id: 1
product_id: 30
updated_at: "2015-11-07 12:37:26"
v_detail: []
variant_name: "Pricing"
But with these query:
$variant = Variant::with('v_detail')->where('product_id','=',$productId)->get();
The result is:
created_at: "2015-11-07 12:37:26"
id: 1
product_id: 30
updated_at: "2015-11-07 12:37:26"
v_detail: [{id: 1, variant_id: 1, variant_dtl_name: "Adult", variant_dtl_price: 25,…},…]
0: {id: 1, variant_id: 1, variant_dtl_name: "Adult", variant_dtl_price: 25,…}
1: {id: 2, variant_id: 1, variant_dtl_name: "Senior", variant_dtl_price: 15,…}
2: {id: 3, variant_id: 1, variant_dtl_name: "Children", variant_dtl_price: 8,…}
variant_name: "Pricing"
Now, on the query that work, how can I fetch a specific column names. Thanks!!
You have this:
$variant = Variant::with(['v_detail' => function($q)
{
// Either add the related foreign key or select all
$q->select('related_foreign_key', 'variant_dtl_name');
}])->where('product_id','=',$productId)->get();
Since you are selecting only a single field which is variant_dtl_name then it's not possible to find out the related models because the relation builder foreign key is required. So, you have to select that foreign key as well. Notice the related_foreign_key in sub-query so use the right one, probably that is variant_id but not sure because you didn't mention anything about that.

How to get hash from array of hashes using a find?

I have a hash set in my code that declares an elegibility scale:
eligibility_scale = [{family_size: 2, minimum_income: 20161, maximum_income: 38200},
{family_size: 3, minimum_income: 25393, maximum_income: 43000},
{family_size: 4, minimum_income: 30613, maximum_income: 47750},
{family_size: 5, minimum_income: 35845, maximum_income: 51600},
{family_size: 6, minimum_income: 41065, maximum_income: 55400},
{family_size: 7, minimum_income: 46297, maximum_income: 59250},
{family_size: 8, minimum_income: 51517, maximum_income: 63050}]
I'm trying to get the hash result using the family_size as the search parameter.
income_bracket = eligibility_scale.select{|h| h["family_size"] == 4 }
# Expecting: {family_size: 4, minimum_income: 30613, maximum_income: 47750}
After running this code I'm getting an empty array.
Any suggestions on what I'm doing wrong?
Hash keys in the question are symbols, not strings.
You need to specify the key as symbol:
Replace following:
h["family_size"]
with:
h[:family_size]
Using select will return an array, first of all, so if your family_size is unique, you can just use find. Otherwise, use select in the same way, but expect to have an array of hashes returned.
Second of all, your hashes are using symbols, not string. Ideally, you're looking at writing something like this:
eligibility_scale.find { |i| i[:family_size] == 4 }
# => {family_size: 4, minimum_income: 30613, maximum_income: 47750}
This is assuming your family size is unique.
You've defined your hash using the key: value syntax which is shorthand for :key => value - meaning the keys are symbols and not strings
eligibility_scale.select {|h| h[:family_size] == 4 }

ActiveRecord query

I have two models Project and UrlList. A project :has_many url_list and a url_list :belongs_to project.
Now I have array for project id's all_projects = [1,2,5,8,16]. I want to retrieve all the records from url_list where project_id is one of those from all_projects array. How do I write code for it?
You can pass an array as value for an attribute to where method:
all_projects = [1, 2, 5, 8, 16]
url_lists = UrlList.where(:project_id => all_projects)
It'll generate SQL query like that:
SELECT `url_lists`.* FROM `url_lists` WHERE `project_id`.`user_id` IN (1, 2, 5, 8, 16)

Kohana Database Query Builder custom sort order ("ORDER BY Field (id, 1, 3, 2)" in MySQL)

I have a set of id's to select, so I request:
$ids = array( 1, 2, 3, 4, 5 );
$q = DB::select('field1', 'field2', 'field3')->
from('work')->
where('field1', 'in', $ids)->execute();
How can I sort them in my custom order, like MySQL's 'ORDER BY Field' do?
Check out DB::Expr
You can use it like so:
->order_by(DB::Expr('FIELD(`field`, 3,1,2)'))
Note, you'll have to manually escape the contents

Resources