phplaravellaravel-5eloquentpluck

Eloquent: Retrieve pluck collection with trashed entries


I want to get all Address IDs, including the trashed ones, with pluck. This is how it is working but I don't get the trashed ones as well:

$clientIDs = Client::pluck('address_id')->all();

This is what I tried already:

$clientIDs = Client::pluck('address_id')
                        ->withTrashed()
                        ->get();

Throws: Method withTrashed does not exist.

How can I use pluck and withTrashed() and solve this problem?


Solution

  • pluck() already finalises the query and retrieves the info from the database for you. So the ->all() call is redundant for that - what it does is convert the Eloquent collection into an array.

    For that reason, you're calling withTrashed() after the query has already been executed. Move them and it'll work:

    $clientIds = Client::withTrashed()->pluck('address_id'); // no need for ->get()