Laravel Artisan Tinker: The ls Command

December 7, 2016 —John Koster

The ls command is a useful command that can be used to learn more any class or instantiated object. It is capable of listing any defined variables, constants, functions, classes, interfaces, traits, properties and methods that might be defined in the current scope or within a class or interface. The following example session demonstrates calling the ls command without any options (this will cause the ls command to only list the defined variables that are in the current scope):

1>>> $someVariable = "Hello";
2=> "Hello"
3>>> $user = new App\User;
4=> App\User {#645}
5>>>
6>>> ls
7Variables: $someVariable, $user

In the previous example you can see that the ls command listed the variables $someVariable and $user. The ls command can be used to learn more about the $user variable; this is because the $user variable is holding a reference to an instance of the App\User class. If we attempt to execute the ls command while supplying any primitive type (such as an integer or string), the ls command will issue an error stating something similar to "Unable to inspect a non-object".

The following example shows the results of using the ls command on an Eloquent model instance. It displays all of the class constants, properties and methods:

1>>> ls $user
2Class Constants: CREATED_AT, UPDATED_AT
3Class Properties: $exists, $incrementing, $manyMethods, $snakeAttributes,
4 $timestamps, $wasRecentlyCreated
5Class Methods: __call, __callStatic, __construct, __get, __isset, __set,
6__toString, __unset, __wakeup, addGlobalScope, addHidden, addObservableEvents
7addVisible, all, append, attributesToArray, belongsTo, belongsToMany,
8cacheMutatedAttributes, can, cannot, cant, clearBootedModels, create,
9created, creating, delete, deleted, deleting, destroy, fill, fillable,
10flushEventListeners, forceCreate, forceDelete, forceFill, fresh,
11freshTimestamp, freshTimestampString, fromDateTime, fromJson,
12getActualClassNameForMorph, getAttribute, getAttributeValue, getAttributes,
13getAuthIdentifier, getAuthIdentifierName, getAuthPassword, getCasts,
14getConnection, getConnectionName, getConnectionResolver, getCreatedAtColumn,
15getDates, getDirty, getEmailForPasswordReset, getEventDispatcher, getFillable
16getForeignKey, getGlobalScope, getGlobalScopes, getGuarded, getHidden,
17getIncrementing, getKey, getKeyName, getMorphClass, getMutatedAttributes,
18getObservableEvents, getOriginal, getPerPage, getQualifiedKeyName,
19getQueueableId, getRelation, getRelationValue, getRelations, getRememberToken
20getRememberTokenName, getRouteKey, getRouteKeyName, getTable,
21getTouchedRelations, getUpdatedAtColumn, getVisible, guard, hasCast,
22hasGetMutator, hasGlobalScope, hasMany, hasManyThrough, hasOne, hasSetMutator
23hydrate, hydrateRaw, is, isDirty, isFillable, isGuarded, isUnguarded,
24joiningTable, jsonSerialize, load, makeVisible, morphMany, morphOne, morphTo,
25morphToMany, morphedByMany, newCollection, newEloquentBuilder, newFromBuilder
26newInstance, newPivot, newQuery, newQueryWithoutScope, newQueryWithoutScopes,
27notifications, notify, notifyVia, observe, offsetExists, offsetGet, offsetSet
28offsetUnset, on, onWriteConnection, push, query, reguard, relationLoaded,
29relationsToArray, removeObservableEvents, replicate, resolveConnection,
30routeNotificationFor, save, saveOrFail, saved, saving,
31sendPasswordResetNotification, setAppends, setAttribute, setConnection,
32setConnectionResolver, setCreatedAt, setDateFormat, setEventDispatcher,
33setHidden, setIncrementing, setKeyName, setObservableEvents, setPerPage,
34setRawAttributes, setRelation, setRelations, setRememberToken, setTable,
35setTouchedRelations, setUpdatedAt, setVisible, syncOriginal,
36syncOriginalAttribute, toArray, toJson, totallyGuarded, touch, touchOwners,
37touches, unguard, unguarded, unsetConnectionResolver, unsetEventDispatcher,
38update, updated, updating, usesTimestamps, with

The ls command also provides many options that allow you to filter the output and narrow the search space. For example, we can view only the properties of the $user Eloquent model like so:

1>>> ls $user --properties
2Class Properties: $exists, $incrementing, $manyMethods, $snakeAttributes,
3 $timestamps, $wasRecentlyCreated

There are many options and search filters that can be used when viewing the information related to an object instance. Use the ? ls command to learn about all of the different options available.

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.