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)
Related
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).
I have persisted a sorted list of ids into the database. Now i want to get the records back based on the persisted id's.
However, the records are coming back in the order of the primary keys not the order of the int[] which I have persisted. Not quite sure how this can be acheived.
I currently have the following:
int[] ids = {8, 1, 5};
var items = from i in ContentPage.All()
where ids.Contains(i.ContentPageId)
select i;
Currently the records are coming out in the order of 1, 5, 8 where I actually want 8,1,5
Database does not obliged to return items in certain order. You can then process them this way:
int[] ids = {8, 1, 5};
var items = (from i in ContentPage.All()
where ids.Contains(i.ContentPageId)
select i).ToList();
var answer = (from id in ids
join item in items
on id equals item.ContentPageId
select item).ToList();
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();
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 }
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