Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Collection::except() with null returns all #47821

Merged
merged 1 commit into from
Jul 24, 2023

Conversation

pniaps
Copy link
Contributor

@pniaps pniaps commented Jul 24, 2023

Similar to Collection::only() which allows null

public function only($keys)
{
if (is_null($keys)) {
return new static($this->items);
}
if ($keys instanceof Enumerable) {
$keys = $keys->all();
}
$keys = is_array($keys) ? $keys : func_get_args();
return new static(Arr::only($this->items, $keys));
}

this change allows to get all records, only or excepts the ones you want. For example, you can write

    public function get($only = null, $except = null)
    {
        return $this->connection->table($this->table)
            ->get()
            ->only($only)
            ->except($except)
            ->all();
    }

instead of

    public function get($only = null, $except = null)
    {
        $collection = $this->connection->table($this->table)
            ->get()
            ->only($only);

        if (!is_null($except)) {
            $collection = $collection->except($except);
        }
        return $collection->all();
    }

So I can quickly get all records of the table ->get();
only the records I want ->get([1,3,5]);
or all except some records ->get(null, [2,4]);

@taylorotwell taylorotwell merged commit 64cf2bb into laravel:10.x Jul 24, 2023
15 checks passed
@GrahamCampbell GrahamCampbell changed the title Collection::except() with null returns all [10.x] Collection::except() with null returns all Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants