0

I have this query

$orderStates = OrderState::listsTranslations( 'states' )->pluck( 'states', 'id' )->toArray();

which will output something like

array:3 [▼
  1 => "waiting"
  2 => "agreed"
  3 => "canceled"
]

I need to skip the first one to be something like

array:3 [▼
      2 => "agreed"
      3 => "canceled"
    ]

How this can be done please?

Yousef Altaf
  • 2,631
  • 4
  • 46
  • 71

2 Answers2

0

There is multiple ways you can achieve this. One that doesn't seem very consistent to me would be to use skip(1) (this expects the query to sort by id and that always the entry with id = 1 needs to be skipped):

$orderStates = OrderState::listsTranslations( 'states' )
    ->skip(1)
    ->pluck( 'states', 'id' )
    ->toArray();

An alternative might be to use whereNotIn() to exclude specific ids:

$orderStates = OrderState::listsTranslations( 'states' )
    ->whereNotIn('id', [1])
    ->pluck( 'states', 'id' )
    ->toArray();

You could also use where('id', '!=', 1) instead of whereNotIn('id', [1]) or skip(1), but personally I think that whereNotIn offers the most extensibility for the future.

Namoshek
  • 6,394
  • 2
  • 19
  • 31
  • Thanks for response but didn't work with me try skip(1) got `SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'offset 1' at line 1 (SQL: select `order_states`.`id`, `order_state_translations`.`states` from `order_states` left join `order_state_translations` on `order_state_translations`.`order_state_id` = `order_states`.`id` where `order_state_translations`.`locale` = ar offset 1)` – Yousef Altaf Apr 22 '18 at 14:30
  • Hm yeah, it is actually a limitation of MySQL you are running into there. If you would add a `limit(100)` (or any other number that is greater than the amount of records you want to receive from the database), it will work. But as this is hacky and adds some functionality you might not be able to debug in future, better stay away from it. – Namoshek Apr 22 '18 at 14:48
0

First thanks for Namoshek guide but skip(1) didn't work with this but slice(1) did

Here is the last query

$orderStates = OrderState::listsTranslations( 'states' )
                                 ->pluck( 'states', 'id' )
                                 ->slice( 1 )
                                 ->toArray();

Worked fine.

Yousef Altaf
  • 2,631
  • 4
  • 46
  • 71