FreshPro
FreshPro

Reputation: 873

Laravel chunk array returns empty

I've just started laravel and when I try to get values from database it's fine. But when I try to return the values, it's just empty. Here's the code:

 public function index()
{
    $collection = [];
    Leg::chunk(200, function ($data) {

        foreach ($data as $val) {
            $collection[] = [
                'fixture_id' => $val->fixture_id,
                'ops_leg_id' => $val->ops_leg_id,
                'designator' => $val->designator,
                'operator' => $val->operator,
                'flight_number' => $val->flight_number,
                'dep_airport' => $val->dep_airport,
                'arr_airport' => $val->arr_airport,
                'ac_registration' => $val->ac_registration,
                'ac_code' => $val->ac_code,
                'sta_at' => $val->sta_at,
                'std_at' => $val->std_at,
                'ata_at' => $val->ata_at,
                'atd_at' => $val->atd_at,
                'deleted' => $val->deleted
            ];


        }
    });
    return array($collection);

}

Edit: My output of the webpage is just [[]]

Upvotes: 0

Views: 694

Answers (1)

cornelb
cornelb

Reputation: 6066

The inline function does not return anything

public function index()
{

    Leg::chunk(200, function ($data) {

        $collection = [];
        foreach ($data as $val) {
            $collection[] = [
                'fixture_id' => $val->fixture_id,
                'ops_leg_id' => $val->ops_leg_id,
                'designator' => $val->designator,
                'operator' => $val->operator,
                'flight_number' => $val->flight_number,
                'dep_airport' => $val->dep_airport,
                'arr_airport' => $val->arr_airport,
                'ac_registration' => $val->ac_registration,
                'ac_code' => $val->ac_code,
                'sta_at' => $val->sta_at,
                'std_at' => $val->std_at,
                'ata_at' => $val->ata_at,
                'atd_at' => $val->atd_at,
                'deleted' => $val->deleted
            ];
        }
        return array($collection);
    });

}

Upvotes: 1

Related Questions