Eloquent ORM
Eloquent: Getting Started
Introduction
Laravel includes Eloquent, an object-relational mapper. Laravel Bash includes Eloquent, a text-relational pipeline. A model runs SQL, and everything after that is small programs passing tab-separated text to each other, with a header row at the top:
Post::all | where status = published | orderBy votes desc | take 3
Defining Models
# app/Models/Post.sh
model Post posts
fillable title body status votes
attributes status=draft votes=0
casts votes=integer
hasMany comments Comment post_id
model defines Post::all, Post::find, Post::count, Post::create and Post::table with eval. Generate one with ./sail artisan make:model Post.
A model file reads like a class body: model names the model, and the lines after it describe it, until the next model.
Mass assignment
fillable lists the columns that create may set, like $fillable. Anything else is an error, not silently ignored:
$ ./sail artisan tinker --execute='Post::create title=Hi body=… id=1'
Add [id] to the fillable property to allow mass assignment on [Post].
warning
This matters more here than in Laravel. The keys you pass to create become column names in the INSERT, and only the values are quoted. fillable is what keeps a posted field name out of your SQL. Use it with form requests, which only pass on the fields they validated.
Default attribute values
attributes sets values for columns that create isn't given, like $attributes.
Attribute casting
casts checks and converts values on the way into the database, like $casts:
| Cast | Accepts | Stores |
|---|---|---|
integer | -?[0-9]+ | the number |
boolean | 1, true, on, yes, 0, false, off, no and empty | 1 or 0 |
Anything else is an error. On the way out, everything is text again, because text is what flows through the pipes.
Retrieving Models
Post::all # every row, ordered by id
Post::find 1 || abort 404 # one row, or exit code 1
Post::count # a number
Building Queries
Every helper reads rows on stdin and writes rows on stdout, so they chain with | in any order:
| Helper | Example | Built with |
|---|---|---|
where | where votes '>' 100 | awk |
orderBy | orderBy votes desc | sort -t $'\t' -k |
take | take 3 | head |
skip | skip 10 | tail -n + |
first | first | head |
only | only id title | awk |
pluck | pluck title | cut -f |
count | count | wc -l |
withCount | withCount comments | uniq -c and join |
table | table | column -t |
toJson | toJson | jq |
> Post::all | where status = published | only id title votes | take 3 | table
id title votes
1 Introducing Laravel Bash 342
2 Why every request gets its own process 128
3 Eloquent is a pipeline 256
where
where is a short awk program. It finds the column's number in the header row, then compares that field on every other row. = and != compare text; >, <, >= and <= compare numbers; like is a case-insensitive substring.
Post::all | where title like bash
note
Quote '>' and '<'. Unquoted, they're redirections, and Bash will create a file called 100 in your project.
orderBy
orderBy takes the header off with read, then hands the rest to sort. If the first value looks like a number, it adds -g to sort numerically, so 9 comes before 10. -s keeps rows with equal values in their original order.
Why there's no select
The helper that keeps only some columns is called only, because select is a Bash keyword: it builds numbered menus.
Lazy Collections
Laravel's lazy collections only load what they need. In Laravel Bash, every query is lazy, thanks to SIGPIPE:
Post::all | where status = published | take 3
take 3 is head -n 4 (the header, then three rows). After four lines, head exits. The next time awk writes to the pipe, nobody is reading it, and the kernel sends awk a SIGPIPE, which ends it. The same happens to sqlite3 behind it. The rest of the table is never read.
Inserting Models
id=$(Post::create title="Hello" body="World" status=draft)
create fills in the default attributes, checks every key against fillable, applies the casts, then runs one INSERT … RETURNING id and prints the id.