Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 288 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert laravel object to array

#11
It's also possible to typecast an object to an array. That worked for me here.

`(array) $object;`

will convert

stdClass Object
(
[id] => 4
)

to

Array(
[id] => 4
)


Had the same problem when trying to pass data from query builder to a view. Since data comes as object. So you can do:

$view = view('template', (array) $object);

And in your view you use variables like

{{ $id }}
Reply

#12
**UPDATE since version 5.4 of Laravel it is no longer possible.**


You can change your db config, like @Varun suggested, or if you want to do it just in this very case, then:

DB::setFetchMode(PDO::FETCH_ASSOC);

// then
DB::table(..)->get(); // array of arrays instead of objects

// of course to revert the fetch mode you need to set it again
DB::setFetchMode(PDO::FETCH_CLASS);

For New Laravel above 5.4 (Ver > 5.4)
see

[To see links please register here]

fetch mode section
```php
Event::listen(StatementPrepared::class, function ($event) {
$event->statement->setFetchMode(...);
});
```
Reply

#13
this worked for me in laravel 5.4

$partnerProfileIds = DB::table('partner_profile_extras')->get()->pluck('partner_profile_id');
$partnerProfileIdsArray = $partnerProfileIds->all();

output

array:4 [▼
0 => "8219c678-2d3e-11e8-a4a3-648099380678"
1 => "28459dcb-2d3f-11e8-a4a3-648099380678"
2 => "d5190f8e-2c31-11e8-8802-648099380678"
3 => "6d2845b6-2d3e-11e8-a4a3-648099380678"
]

[To see links please register here]

Reply

#14
Object to array

$array = (array) $players_Obj;

$object = new StdClass;
$object->foo = 1;
$object->bar = 2;

var_dump( (array) $object );

Output:

array(2) {
'foo' => int(1)
'bar' => int(2)
}
Reply

#15
Use the `toArray()` method to convert an object to array:

```
$foo = Bar::first(); // Get object
$foo = $foo->toArray(); // Convert object to array
```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through