Eloquent lar laravel 5 Obtener matriz de ids
Estoy usando Elocuente ORM laravel 5.1, quiero devolver una matriz de id mayor que 0, Mi modelo llamado test
.
He intentado :
$test=test::select('id')->where('id' ,'>' ,0)->get()->toarray();
Devuelve:
Array ( [0] => Array ( [id] => 1 ) [1] => Array ( [id] => 2 ) )
Pero quiero que el resultado sea en una matriz simple como:
Array ( 1,2 )
39
4 answers
Usted podría utilizar lists()
:
test::where('id' ,'>' ,0)->lists('id')->toArray();
NOTA: Mejor si define sus modelos en formato Studly Case
, por ejemplo Test
.
También Se podría utilizar get()
:
test::where('id' ,'>' ,0)->get('id');
ACTUALIZAR: (Para versiones >= 5.2)
La lists()
el método fue obsoleto en las nuevas versiones >= 5.2
, ahora usted puede utilizar pluck()
método :
test::where('id' ,'>' ,0)->pluck('id')->toArray();
104
Author: Zakaria Acharki,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-07-28 17:31:32
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-07-28 17:31:32
Lea acerca del método lists ()
$test=test::select('id')->where('id' ,'>' ,0)->lists('id')->toArray()
4
Author: Amir Bar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-12-16 09:32:13
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-12-16 09:32:13
La respuesta correcta a eso es el método lists
, es muy simple como esto:
$test=test::select('id')->where('id' ,'>' ,0)->lists('id');
Saludos!
3
Author: Radames E. Hernandez D.L.R.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-29 16:59:06
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-29 16:59:06
También puede usar el método all() para obtener una matriz de atributos seleccionados.
$test=test::select('id')->where('id' ,'>' ,0)->all();
Saludos
0
Author: Narendra Ojha,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-14 08:30:18
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-14 08:30:18