Convert Array of objects to array of integers - laravel-5

I need to convert the following array of objects to array of integers in laravel. Below is the code which I need to convert as an array.
$idArray = DB::table('booking_header')
->select('booking_header.id')
->where('booking_header.parent_booking_id', $oldId)
->get();
I tried with pluck(). But it doesn't work. Can anyone help me?

Use pluck() insted of get() to get a simple array of ids.
$idArray = DB::table('booking_header')
->select('booking_header.id')
->where('booking_header.parent_booking_id', $oldId)->pluck('booking_header.id');

Related

DB::get is Array in Laravel but it says it is not array

I thought the data which is from DB::get() is Array.
However , the console says it is not array.
$fruitList = Food::where('id' => 300)->get(['id']);
shuffle($fruitList);
ErrorException: shuffle() expects parameter 1 to be array, object given in file
The return value of get() is not an array. it's Laravel array collection you can convert it to an array or use shuffle of array collection:
$fruitList = Food::where('id' => 300)->get(['id'])->toArray();
shuffle($fruitList);
with array collection:
$fruitList = Food::where('id' => 300)->get(['id'])->shuffle();
Just like #A.seddighi mentioned, using get() or all() gives you a collection. It may seem like an array when you output it using return or print but it is different.
Collections can be filtered, queried and so on. e.g
$fruitList->has('price')
etc.
To get an array simply called the toArray() method on it, you may also use flatMap(), mapWithKeys() etc. Make sure you follow the documentation that is suitable for your version of laravel.

read manipulate and write to the db in Laravel

I am going to read the data by query, manipulate some values and write it back to the db. I use the code below but I get an error.
$data = DB::table('users')->get()->toArray();
foreach ($data as $d){
$d->id = $id+100;
DB::table('users')->insert($d);
}
Argument 1 passed to Illuminate\Database\Query\Builder::insert() must be of the type array,
but the input is already an array. Do you have a better solution for this?
Ok. let's explain this, what ->toArray() actually did is converting the whole collection to array not casting the selected records to array so if you dd($data) you will find it's an array of objects not array of arrays, so what you need to do is to cast each record in the selected records like so
$data = DB::table('users')->get()->map(function ($user) {
return (array) $user;
})->toArray();
as i said
->toArray() - this will convert the whole collection to array
(array) $user - this will convert the selected record to array
you can simply use increment function
DB::table('users')
->increment('id', 100);
more info : laravel docs

Laravel Eloquent ORM - How to get the all the properties of afrom/of a collection in a array?

$user_emails = ["email_1#domain.com", "email_2#domain.org"];
$users = Users::whereIn("email", $user_emails);
The table for users also has a phone column for each user. What's the best way to get a list/array of the phone number as an array?
$users->all()->phone(); // Like (which is not correct)
Try to use get() like :
$users = Users::whereIn("email", $user_emails)->get(['phone'])->toArray();
Or also pluck() like :
$users = Users::whereIn("email", $user_emails)->pluck('phone')->all();
Hope this helps.
Use pluck method to fetch a specific column's values and then use toArray on returned Collection object to get results as an array.
$phoneNumbers = Users::whereIn("email", $user_emails)->pluck('phone')->toArray();
You can get all column data with get()
Example:
$user = $user::where('email', $user_emails)->get();
You can get the list with foreach loop method.

Extract only raw ids from an Eloquent collection

I need to get an array of integers, which will contain raw ids of Eloquent records. The following query
Quote::select('id')->get();
gets an eloquent collection instead of an array of integers. How to modify it, so it meets my requirements?
Try this code:
Quote::get(['id'])->toArray();
Or:
Quote::all(['id'])->toArray();
Or:
DB::table('quote')->select('id')->get()->toArray();
Or:
Quote::pluck('id')->toArray();

Laravel Queries: 'lists' method

The 'lists' method surely does retrieve a list of column's values, but I want the method to retrieve the values but without duplication, how to achieve that?
$types = DB::table('events')->lists('type');
If you need to get just distinct values, you should do:
$types = DB::table('lists')->distinct()->lists('type');

Resources