SQL pipeline
This page uses the simulated rentals data. It does not describe real rentals.
This example runs one phplyr pipeline in two ways. On a lazy phibble table, created with ->phibble() on an Eloquent query, the pipeline builds a single SQL query that the database runs. On an in-memory DataFrame, created with ->toDataFrame(), the same pipeline runs in PHP. The page shows the generated SQL and both results.
The pipeline describes columns with col() expressions, which phibble can translate to SQL. A PHP closure in a verb would make phibble load the rows into memory first. Eloquent casts and accessors do not apply to a lazy table, because its rows come straight from SQL.
Code
$pipeline = fn ($rentals) => $rentals |>
filter(col('bedrooms')->ge($minBedrooms)) |>
mutate(rent_per_m2: col('monthly_rent')->div(col('area_m2'))) |>
group_by('neighborhood') |>
summarise(rentals: n(), mean_rent_per_m2: mean('rent_per_m2')) |>
arrange(desc('mean_rent_per_m2'));
$sql = SimulatedRental::query()->phibble() |> $pipeline;
$sql |> show_query();
$memory = SimulatedRental::query()->toDataFrame() |> $pipeline;
$pipeline = fn ($rentals) => phplyr($rentals)
->filter(col('bedrooms')->ge($minBedrooms))
->mutate(rent_per_m2: col('monthly_rent')->div(col('area_m2')))
->groupBy('neighborhood')
->summarise(rentals: n(), mean_rent_per_m2: mean('rent_per_m2'))
->arrange(desc('mean_rent_per_m2'))
->get();
$sql = $pipeline(SimulatedRental::query()->phibble());
phplyr($sql)->showQuery();
$memory = $pipeline(SimulatedRental::query()->toDataFrame());