-2

i want to export data from my json to csv file but i get this error Cannot use object of type stdClass as array

i want to know please how i can use it as array

public function exportUsers()
 {
  $users = ServicePoint::all()->where("nature", "SP")->toArray();
  $users = ServicePoint::all()->where('statut','<>', 2);

  $arrayCsv = [];
  foreach ($users as $key => $line){
   $arrayCsv[$key][] = $line['name'];
   $arrayCsv[$key][] = $line['lastname'];
   $arrayCsv[$key][] = $line['email'];

  
  }

anything can help please

  • Possible duplicate of https://stackoverflow.com/questions/6815520/cannot-use-object-of-type-stdclass-as-array – 04FS May 24 '19 at 11:31
  • 2
    Possible duplicate of [Cannot use object of type stdClass as array?](https://stackoverflow.com/questions/6815520/cannot-use-object-of-type-stdclass-as-array) – MrMaavin May 24 '19 at 11:46

3 Answers3

1

I am assuming you are trying to get users based on 2 where clauses.

If so, try this

$users = ServicePoint::all()->where("nature", "SP")
                            ->where('statut','<>', 2)
                            ->get();

Then change these lines

$arrayCsv[$key][] = $line->name;
$arrayCsv[$key][] = $line->lastname;

and so on.

linuxartisan
  • 2,396
  • 3
  • 21
  • 40
1
public function exportUsers() {
    $users = ServicePoint::where("nature", "SP")->where('statut','<>', 2)
->all()->toArray();
    $arrayCsv = [];
    foreach ($users as $key => $line){
        $arrayCsv[$key][] = $line['name'];
        $arrayCsv[$key][] = $line['lastname'];
        $arrayCsv[$key][] = $line['email'];
    }
}

Try this, it should be working.

Vishal Tarkar
  • 808
  • 11
  • 32
1

As its object it can be accessible with -> operator

foreach ($users as $key => $line) {
    $arrayCsv[$key][] = $line->name;
    $arrayCsv[$key][] = $line->lastname;
    $arrayCsv[$key][] = $line->email;
}

Should work.

Rahul
  • 18,271
  • 7
  • 41
  • 60