Laravel collection opposite of diff - laravel

i have 2 collections:
$collection = collect([1,2,3,4]);
$collection2 = collect([1,5,6,4]);
with:
$collection->diff($collection2);
i get [2,3]
so what i want is to have the opposite value, [1,4]
Is there a way or method in collection to make this?

You can use duplicates method after mergging into single collection
$collection1 = collect([1,2,3,4]);
$collection2 = collect([1,5,6,4]);
$duplicates = $collection1
->merge($collection2)
->duplicates()
->values();
Will give you
Illuminate\Support\Collection {#1151
all: [
1,
4,
],
}

laravel collection intersect
intersect method removes any values from the original collection that are not present in the given array or collection.
$collection = collect([1,2,3,4]);
$collection2 = collect([1,5,6,4]);
$intersect = $collection->intersect($collection2);
$intersect->all();

Related

Combine 2 collections (keep the similar ones)

I've several collections, I want to keep only the elements that are present in each collection.
I went through the available methods, but I didn't find anything that would match.
$candidatesByConsultant = Consultant::find(request('consultant_id'))->candidates;
$candidatesByCreation = Candidate::whereBetween('created_at',[Carbon::parse(request('meeting_since')), Carbon::parse(request('meeting_to'))])->get();
Do you have any idea? :)
In order to have values that only present in both collection you must use intersect method:
$result = $candidatesByConsultant->intersect($candidatesByCreation);
The intersect method intersects the values of both collections. You can read it in Laravel's official documentation.
And in order to get have results that are not present in both collection you must use diff method:
$result = $candidatesByConsultant->diff($candidatesByCreation);
The diff method finds differences between collections. You can read it in Laravel's official documentation.
The intersect method may be suitable : https://laravel.com/docs/5.8/collections#method-intersect
Example taken from the documentation:
$collection = collect(['Desk', 'Sofa', 'Chair']);
$intersect = $collection->intersect(['Desk', 'Chair', 'Bookcase']);
$intersect->all();
// [0 => 'Desk', 2 => 'Chair']
However, especially if you are trying to intersect multiple collections of Eloquent models, it may not work since the equality between two models is defined by the Model::is() method. Check
https://laravel.com/docs/5.8/eloquent#comparing-models for more information about comparing two Eloquent models.
To handle this, I would do the following, assuming the primary key of your models is id:
$candidatesByConsultant = Consultant::find(request('consultant_id'))->candidates;
$candidatesByCreation = Candidate::whereBetween('created_at',[Carbon::parse(request('meeting_since')), Carbon::parse(request('meeting_to'))])->get();
$candidates = $candidatesByConsultant->merge($candidatesByCreation)->unique("id");
You may check the merge() and unique() documentations.
The built-in for this is $collection->intersect($other), but you can also achieve the desired result with a simple custom filter:
$left = collect([Model::find(1), Model::find(2), Model::find(3)]);
$right = collect([Model::find(1), Model::find(3), Model::find(5)]);
$result = $left->filter(function ($value, $key) use ($right) {
return $right->contains(function ($v, $k) use ($value) {
return $v->id === $value->id;
});
});
This will perform model comparison by id. It is not very performant though. Another approach would be to retrieve two arrays of ids, intersect them and filter the merged sets based on this list:
$left = collect([Model::find(1), Model::find(2), Model::find(3)]);
$right = collect([Model::find(1), Model::find(3), Model::find(5)]);
$merged = $left->merge($right);
$ids = array_intersect($left->pluck('id')->toArray(), $right->pluck('id')->toArray());
$result = $merged->filter(function ($value, $key) use ($ids) {
return in_array($value->id, $ids);
});

Laravel Model::find() auto sort the results by id, how to stop this?

$projects = Project::find(collect(request()->get('projects'))->pluck('id')); // collect(...)->pluck('id') is [2, 1]
$projects->pluck('id'); // [1, 2]
I want the result to be in the original order. How do I achieve this?
Try $projects->order_by("updated_at")->pluck("id"); or "created_at" if that's the column you need them ordered by.
Referencing MySQL order by field in Eloquent and MySQL - SELECT ... WHERE id IN (..) - correct order You can pretty much get the result and have it order using the following:
$projects_ids = request()->get('projects'); //assuming this is an array
$projects = Project::orderByRaw("FIELD(id, ".implode(',', projects_ids).")")
->find(projects_ids)
->pluck('id'));
#Jonas raised my awareness to a potential sql injection vulnerability, so I suggest an alternative:
$projects_ids = request()->get('projects');
$items = collect($projects_ids);
$fields = $items->map(function ($ids){
return '?';
})->implode(',');
$projects = Project::orderbyRaw("FIELD (id, ".$fields.")", $items->prepend('id'))
->find($projects_ids);
The explanation to the above is this:
Create a comma separated placeholder '?', for the number of items in the array to serve as named binding (including the column 'id').
I solve this by querying the data one by one instead mass query.
$ids = collect(request()->get('projects'))->pluck('id');
foreach($ids as $id){
$projects[] = Project::find($id);
}
$projects = collect($projects);
$projects->pluck('id');
I have to do this manually because laravel collection maps all the element sorted by using ids.

Two column values in same table Laravel 5.4 merge into Array

I want to merge example_1 and example_2 values into array.
example_1 and example_2 are of type int.
User::select('example_1','example_2')->where('id',Auth::user()->id)->get();
// The result: [{"example_1":"1","example_2":"2"}]
example_1 and example_2 if has value 1 and 2 respectively.
I want to have an array : [1,2]
You can do one of this
$result = array_only(auth()->user()->toArray(), ['example_1','example_2']);
// Or
$result = User::where('id', auth()->id())->first(['example_1','example_2'])->toArray();
// Finally
$data = array_values($result);
All Eloquent queries return Collection objects. Those can be modified easier (through collections methods).
You can use first() to get the first object out of this collection.
Now you can access the variables of the Eloquent User model by calling them like you'd call public class properties.
Your final code would look like:
$user = User::select('example_1','example_2')
->where('id',Auth::user()->id)->get()->first();
$array = [ $user->example_1, $user->example_2 ];
// The result: [1,2]

get the first, the second, the third element of collection

I have a collection like that:
Collection {#750 ▼
#items: array:18 [▼
18 => User {#691 ▶}
19 => User {#696 ▶}
20 => User {#701 ▶}
]
}
Where 18, 19, 20 should vary
I tried to call with
$collection->get(0);
$collection->get(1);
$collection->get(2);
But obviously, it doesn't work
I found a workaround with shift, that return first element and remove it from the collection,
$el1 = $collection->shift();
$el2 = $collection->shift();
$el3 = $collection->shift();
But in this case, my original collection is destroyed.
Any idea how I should do it?
You can use slice() method:
$collection->slice(0, 3)
The slice method returns a slice of the collection starting at the given index. If you would like to limit the size of the returned slice, pass the desired size as the second argument.
You can use values() as:
$collection = $collection->values();
then you can use it as:
$collection->get(0);
$collection->get(1);
$collection->get(2);
You can either loop over them:
foreach ($collection as $element) {
// use $element
}
Or you can reset the keys to be sequentially indexed:
$collection = $collection->values();
$element1 = $collection->get(0);
$element2 = $collection->get(1);
$element3 = $collection->get(2);
There are many ways you can access thoes values. For example, you can get keys by calling:
$keys = $collection->keys()
Then call by the ordered keys you wanted:
if (isset($keys[0])) $collection->get($keys[0]);

Manual paginator on merged collection

I have a merged collection I want to paginate and I can't seem to figure this out.
First off, the reason I need to create the paginator manually is because I fetch 2 collections first and merge them, like so:
$a = ModelA::all();
$b = ModelB::all();
$c = $a->merge($b);
Next step is to paginate this collection, I tried the Paginator and the LengthAwarePaginator
PAGINATOR
$page = 1;
$results = new Paginator($c, 2, $page);
Here I get the 2 first results in a paginator
$page = 2;
$results = new Paginator($c, 2, $page);
I still get the 2 first results, while i'd expect the third and fourth result (the collection is more then 2 elements long!)
LENGTHAWAREPAGINATOR
$page = 1;
$results = new LengthAwarePaginator($c, count($c), 2, $page);
Here I get a Paginator but the items contain all elements of the collection, no matter what page number i ask for (instead of the 2 i'm asking for)
Any ideas on what might be the problem?
Thank you in advance
According to the Laravel community this is intentional.
You should slice your items before passing it to the Paginator.

Resources