Laravel group by 1 order by multiple - laravel

I want to get a collection, group it by 1 field, and order it by multiple fields.
Here are things I've tried:
$permission_groups = collect(Permission::orderBy('group', 'asc')->orderBy('name', 'asc')->get());
$permission_groups->groupBy('group');
Causes Trying to get property of non-object
$permission_groups = Permission::all()->sortBy('group', 'asc')->sortBy('name', 'asc')->groupBy('group');
Causes the collection to be sorted and then resorted.
How do I simply order by 2 columns and group it?

Figured it out:
$permission_groups = Permission::orderBy('group', 'asc')->orderBy('name', 'asc')->get()->groupBy('group');

Related

count get collection and count query giving different results

So i am trying to get distinct collections like this
$data = DB::table('project_1_data')->distinct('Farmer_BankVerificationNumber_Farmer')->get();
But when i do a count($data) I get 1600 but when i run
$data = DB::table('project_1_data')->distinct('Farmer_BankVerificationNumber_Farmer')->count();
I get 1440. This is weird as i only want collection with distinct field 'Farmer_BankVerificationNumber_Farmer'. How do i write the query correctly?
It's because you're asking your query to count the distinct values in the wrong place.
You need to tell your count parameter what field you would like to count on. So you'll basically ask the query to get the database information, separate the distinct values and then count how many of a specific field are distinct.
Your finished query should look like this:
$data = DB::table('project_1_data')
->distinct()
->count('Farmer_BankVerificationNumber_Farmer');

$operations = Operation::findOrFail(2)->get(); return 6 results

I'm trying to get the operation that as PK id = 2
$operations = Operation::findOrFail(2)->get();
This line should do the trick, but I get all my 6 operations as result...
Off course, my other 5 has ID value from 1 to 6, they doesn't match.
I checked Operation is a model with no custom config, and Operation table has been created by migration and has ID as PK...
Off course, I could change it with another eloquent query, but this should work and I would love to know why it is failing.
What am I missing ?
remove ->get() it will should be
$operations = Operation::with('meters')->findOrFail(2);
get() gives you a collection which is all data of Operation to get single instace you should remove get()

Laravel Eloquent - How do I select columns of the updated row?

I'm trying to update a single row,
using this query,
$update_row = PayRecord::where('date_from',$date_from)
->where->('date_to',$date_to)
->update(['status',1]);
what I want to do is to get the id of the updated row, without creating another query to get the id,
$get_updated_row_id = PayRecord::where('date_from',$date_from)
->where->('date_to',$date_to)'
->get('id');
Thank You,
Remove extra -> from where clause.
This may useful to you.
$update_row = PayRecord::where('date_from',$date_from)
->where('date_to',$date_to)
->first();
$update_row->update(['status',1]);
$update_row->id; // to get the id

Filtering resultset with codeigniter and datamapper

I got a little problem with datamapper, and I'd like to know if there is a quick solution.
Let's say I have this kind of data
Groups table
id | Name
1 | admin
2 | guest
3 | editor
4 | moderator
In my base controller I set a global field to see only the groups that are not admin
$this->groups_ = new Group();
$this->groups_->where('id >', 1)->get();
//so I can select the users that are not admin
$users = new User();
$users->where_related('group',$id,$this->groups_)->get();
Now in my controllers I'd like to filter the groups. For example I want to select only editors and guests (id between 1 and 4). So I would like to filter the initial result set... something like this
$this->groups_->where('id <',4)->get();
But it doesn't work. It returns ALL the group ids < 4 including admin.
What would be the right way to get this?
Well, datamapper does not query objects or groups of objects. It queries the database. So when you do the second get(), you're executing a separate query on the database.
You can do this, but it will be an additional query:
$this->groups_->where('id >', 1)->where('id <', 4)->get();
or you can loop through $this->groups_ after your first query in php and filter the set there and save as an array.
A possible solution
Maybe I found a possible workaround by cloning the datamapper object.
What I want to do is not to query the db multiple times, but I'd like to have a base resultset and refine the results.
So in my base controller I would do something like
$this->groups_ = new Group();
$this->groups_->where('id >', 1);//without getting the results
//so I can select the users that are not admin
$users = new User();
//here i'm getting the results from the clone
$users->where_related('group',$id,$this->groups_->get_clone()->get())->get();
And, as I left the object "open", without getting the results, I can use it in my controllers, as a base for my other "queries"... ok,they are actually new conditions of the first query.
//now the query return the groups between 2, as I set in the base controller
//and 4, set in the child controller
$this->groups_->where('id <',4)->get();

Apply filter on collection Object

I am getting this object on list page
$_productCollection=$this->getLoadedProductCollection();
//return 3 records
now I applied filter as
$_productCollection=$_productCollection->addFieldToFilter('genre', array('finset' => '126'));
//now it should return 1 record
but it gives me a count of 3. Now, if I run the query in database by getting the query using echo $_productCollection->getSelect(); it returns 1 record.
Can anybody help me to resolve this?
Most likely this doesn't work because $this->getLoadedProductCollection() returns a collection which already has been loaded by the catalog/layer singleton.
But you could override Mage_Catalog_Model_Layer::prepareProductCollection() to get in control and add the custom filters you want.

Resources