how to merge 2 same collections in larave - php

consider on first query i get 2 rows and on 2nd query i get 1 row i want to 2+1 = 3 rows into a single collection so that i can have all the data here is my query
$invoices = Invoice::whereIn('user_id', $ids)->get(); //have data in column id 1 and 2
$stock = Invoice::where('customer_id' , Auth::id())->get(); // have data in column id 3
$merge = //how i can merge this?
now consider i merged both my output should be using foreach
foreach($merge as $a){
$b[] = $a->id;
}
dd($b);
output should be [1,2,3]

you can get the both result in one query using orWhere:
$mergedResult=Invoice::whereIn('user_id', $ids)->orWhere('customer_id' , Auth::id())->get();

You can do this in one query, BUT if you actually want to merge these 2 Eloquent Collections:
$new = $invoices->merge($stock);
Surprise, there is a merge method.
Laravel 8.x Docs - Collections - Available Methods - merge

Related

Laravel count distinct value and get as associative array of key value pair

I have a field called language in my table submission. Here the different user has submitted their problem with different language such as java(56) 5 times, CPP(45) 7 times, and python(71) 10 times.
I want to have a query in laravel with eloquent such that it returns an array or with key-value pair as
$user_lang = ['56'=>5,'45'=>7,'71'=>10]
here 56,45,71 are the id of the languages
If you have prepared data like count of how many times, you can simply use pluck method. eg
$user_lang = Language::where(<condition>)->pluck('times', 'id');
It will return the array as you desired.
If you do not have prepared count, then count it using group by and simply use same pluck method .
$result = Submission::selectRaw('id', 'COUNT(DISTINCT id) AS times'))
->groupBy(id)
->get();
$user_lang = [];
$result->map($result, function($item){
$user_lang[$item->id] = $item->times;
});

how to compare two collection and get match records

I am using Laravel and I have two different collections that contain ID of products
First one is colorProduct and second is tagProduct
so I want to compare these two and get only same ID of products so how can I do this?
$colorProducts = Color::where('code', $request->color)->get()->first()->products;
$tagProducts = $tag->products->where('shop_id', $shop->id);
$colorAndTagProducts = collect();
foreach ($colorProducts->toBase()->merge($tagProducts)->unique('id')->groupBy('id') as $allProducts) {
if ($allProducts->count() >= 1) {
$colorAndTagProducts[] = $allProducts->first();
}
}
here
$colorAndTagProducts
gives me all records form both collection but I only want same record
I dont know, if I understand correctly, but maybe like this?
I suppose Color and Product are in many to many relationship. And Product and Shop/tag in one to many.
$colorId = Color::where('code', $request->color)->get()->first()->id;
$shopId = $shop->id;
$products = Product::whereHas('colors', function ($query) use ($colorId) {
$query->where('id', $colorId); //id or color_id
})->where('shop_id', $shopId)->get();
intersect()
The intersect method removes any values from the original collection
that are not present in the given array or collection. The resulting
collection will preserve the original collection's keys:
I did it with this method

Combine two queries into one and sort them

I am trying to display buildings and circuits in list (only 3 in total, sorted by the column "updated_at"). The problem is that they are not in the same table. I have a table circuits and a table buildings.
I tried to do this:
$buildings = Building::published()->limit(3)->get();
$circuits = Circuit::published()->limit(3)->get();
$merged = $buildings->merge($circuits);
$this->data['buildingsAndCircuits'] = $merged->all();
I get everything right when I'm doing my var_dump and I know how to access the data when I do a foreach. But that does not do what I want.
I would like to sort them (by updated_at) and have only three and not six.
Is there a way to do that?
I tried to make conditions like with the QueryBuilder on $merge but it does not work
$this->data['buildingsAndCircuits'] = $merged->orderBy('updated_at', 'DESC')->limit(3)->all();
thank you very much
Once you've called get on each query then the query is executed and the result is returned. orderBy will not longer work after that since what you have in $buildings, $circuits and $merged is a collection.
You can however do this:
$buildings = Building::published()->limit(3)->get();
$circuits = Circuit::published()->limit(3)->get();
$merged = $buildings->merge($circuits)->sortByDesc('updated_at');
$this->data['buildingsAndCircuits'] = $merged->all();
Check what else you can do on collections in the documentation

Laravel relations, select next raleted row from database.

I have related items in my database. I selected all of items from database by related id:
$next_stock = $this->model->get()->where('part_id', $in_data['part_id'])->all();
and I collection of rows grouped by one specific id, like on the picture. All of them selected by "part_id":
Selection Of Items
Grouped By Same Id
Also with this line of code i can select one of the items from this collection:
$next_stock = $this->model->get()->where('id', $old_stock['id'])->where('part_id', $in_data['part_id'])->first();
But how can I select the following items after this one?
Or, how can I select second or third item from this collect?
I cannot just increase id number by one from first, because sometimes this item ids not following each other.
Having a collection, you can take a specific element in the position with a combination of take() and last().
$collection = $this->model->get()->where('part_id', $in_data['part_id'])->all();
$second = $collection->take(2)->last(); //if this doesnt work, do it in 2 steps
$third = $collection->take(3)->last(); //if this doesnt work, do it in 2 steps
If you don't have a collection, take directly from database like this
$second = $this->model
->where('part_id', $in_data['part_id'])
->skip(1)
->first();
If it doesn't work with first()
$collect = $this->model
->where('part_id', $in_data['part_id'])
->skip(1)
->take(1)
->get();
$second = $collect->first();
Edit
skip() and take() are actually part of the query builder, not eloquent model. So it won't work with Eloquent in Laravel 5.4
Try with
$collect = $this->model
->where('part_id', $in_data['part_id'])
->get(1); //For the second record, 0 being the first
If you aren't doing it yet, you should set your model's relationships.
E.g. If you use "one-to-many", Eloquent will automatically determine the proper foreign key column on the model for you.
$parts = App\Stock::find(1)->partId;
foreach ($parts as $part) {
//
}

How do I perform a Mass delete using Laravel 4.1, based on array of ids or objects?

I just wanted to know if it's possible.
I know when you have multiple rows to insert, you can just build an array and do something like:
DB::table('some_table')->insert($array);
But as far as I've read, doing the same for deleting doesn't seem to be possible, I'd like to know if anyone know of a way to do something like:
DB::table('some_table')->delete($array);
Many ways of deleting records in Laravel 4.1
1) When you want to delete records from your database, simply call the delete method:
$affected = DB::table('users')->where('id', '=', 1)->delete();
2) Want to quickly delete a record by its ID? No problem. Just pass the ID into the delete method:
$affected = DB::table('users')->delete(1);
3) If you want to delete multiple records by id at once, passing their ids in an array - use the following
$users_to_delete = array(1, 2, 3);
DB::table('users')->whereIn('id', $users_to_delete)->delete();
4) If you want to delete multiple records by id at once, passing an array of users - use the following
//(case A) User fields indexed by number 0,1,2..
$users_to_delete = array(
'0'=> array('1','Frank','Smith','Whatever'),
'1'=> array('5','John','Johnson','Whateverelse'),
);
$ids_to_delete = array_map(function($item){ return $item[0]; }, $users_to_delete);
DB::table('users')->whereIn('id', $ids_to_delete)->delete();
//(case B) User fields indexed by key
$users_to_delete = array(
'0'=> array('id'=>'1','name'=>'Frank','surname'=>'Smith','title'=>'Whatever'),
'1'=> array('id'=>'5','name'=>'John','surname'=>'Johnson','title'=>'Whateverelse'),
);
$ids_to_delete = array_map(function($item){ return $item['id']; }, $users_to_delete);
DB::table('users')->whereIn('id', $ids_to_delete)->delete();
5) Deleting An Existing Model By Key
User::destroy(1);
User::destroy(array(1, 2, 3));
User::destroy(1, 2, 3);
6) Of course, you may also run a delete query on a set of models:
$affectedRows = User::where('votes', '>', 100)->delete();

Categories