Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

gizmojo's avatar

Console command with choice using eloquent

I'm trying to show choice option using eloquent.

So far I do this but it doesn't work for non-unique names. Is there a better way of doing this?

$industries = Industry::get();
$choice = $this->choice('Which industry?', $industries->pluck('name')->toArray());
$industry = $industries->firstWhere('name', $choice);
0 likes
1 reply
crnkovic's avatar
crnkovic
Best Answer
Level 43

Well, because it's non-unique, it's kinda bad UX to show:

Which industry?
IT
IT
IT

I, as a user, would have no idea why it's the same line written 3 times. I recommend adding a bit more info, making it unique, like:

Whch industry?
IT (#123)
IT (#22424)
IT (#industry-id-here)

Please or to participate in this conversation.