Laravel Companion
Free!
With Membership
$9.99
Suggested price

Laravel Companion

A Guide to Helpers, Collections And More

About the Book

The Laravel framework contains many useful helper functions, features and utility classes (such as Collections) - not all of these are well known. This book exposes these helper utilities to encourage accelerated application development using the Laravel framework (version 5.3). Through the Laravel Companion you will learn about collections, facades, application helper functions, request and URL helper functions and much, much more.

If you've ever thought there "has to be an easier way" - there probably is in the form of a helper utility.

  • Share this book

  • Categories

    • Computers and Programming
    • Laravel
    • Reference
    • Textbooks
    • Web Development
  • Feedback

    Email the Author(s)

About the Author

Johnathon Koster
Johnathon Koster

Hello, internet!

My name is John, and I have over fifteen years of professional software development. I've worked on a wide range of projects over the years, from simple websites and critical government reporting applications all the way to high-performance telematics solutions.

During my downtime, I enjoy working on open-source projects, writing posts on my blog, and contributing to the Laravel and Statamic ecosystems.

Some of the recent open-source projects I've worked on include:

  • blade-parser-typescript - a Laravel Blade parser, formatter, reflection library, and linter, written in TypeScript
  • Antlers Toolbox - a Visual Studio Code extension providing syntax highlighting, code auto-complete, intelligent refactoring, and project-aware assistance for Statamic developers
  • Antlers Runtime - an advanced PHP templating engine for the Statamic CMS platform
  • Proteus - a Laravel package that makes it simple to read, write, and modify Laravel configuration files from your application code
  • Primitives - a PHP library that safely parses input strings into native PHP types (string, bool, arrays, etc.) and can optionally be configured with a sandbox for method evaluation

Table of Contents

  •  
    •  
      • Who is This Book For?
      • Symbols Used In This Book
      • What Was Used to Write This Book?
  • I Helper Functions
    • 1. String Helper Functions
      • 1.1 Immutable Strings
      • 1.2 ascii($value)
      • 1.3 studly($value)
      • 1.4 camel($value)
      • 1.5 snake($value, $delimiter = '_')
      • 1.6 title($value)
      • 1.7 slug($title, $separator = '-')
      • 1.8 contains($haystack, $needles)
      • 1.9 endsWith($haystack, $needles)
      • 1.10 startsWith($haystack, $needles)
      • 1.11 finish($value, $cap)
      • 1.12 is($pattern, $value)
      • 1.13 length($value)
      • 1.14 limit($value, $limit = 100, $end = '...')
      • 1.15 words($value, $words = 100, $end = '...')
      • 1.16 lower($value)
      • 1.17 upper($value)
      • 1.18 plural($value, $count = 2)
      • 1.19 singular($value)
      • 1.20 singular Special Cases
      • 1.21 random($length = 16)
      • 1.22 quickRandom($length = 16)
      • 1.23 randomBytes($length = 16)
      • 1.24 equals($knownString, $userInput)
      • 1.25 parseCallback($callback, $default)
      • 1.26 replaceFirst($search, $replace, $subject)
      • 1.27 replaceLast($search, $replace, $subject)
      • 1.28 substr($string, $start, $length = null)
      • 1.29 ucfirst($string)
      • 1.30 trans($id = null, $parameters = [], $domain = 'messages', $locale = null)
      • 1.31 trans_choice($id, $number, array $parameters = [], $domain = 'messages', $locale = null)
      • 1.32 e($value)
    • 2. Array Helper Functions
      • 2.1 Immutable Arrays
      • 2.2 “Dot” Notation
      • 2.3 add($array, $key, $value)
      • 2.4 build($array, callable $callback)
      • 2.5 divide($array)
      • 2.6 collapse($array)
      • 2.7 dot($array, $prepend = '')
      • 2.8 except($array, $keys)
      • 2.9 only($array, $keys)
      • 2.10 flatten($array)
      • 2.11 fetch($array, $key)
      • 2.12 first($array, callable $callback = null, $default = null)
      • 2.13 last($array, callable $callback = null, $default = null)
      • 2.14 has($array, $key)
      • 2.15 set(&$array, $key, $value)
      • 2.16 forget(&$array, $keys)
      • 2.17 get($array, $key, $default = null)
      • 2.18 pull(&$array, $key, $default = null)
      • 2.19 pluck($array, $value, $key = null)
      • 2.20 where($array, callable $callback)
      • 2.21 sort($array, callable $callback)
      • 2.22 isAssoc(array $array)
      • 2.23 accessible($value)
      • 2.24 exists($array, $key)
      • 2.25 prepend($array, $value, $key = null)
    • 3. Application and User Flow Helper Functions
      • 3.1 app($make = null, $parameters = [])
      • 3.2 auth($guard = null)
      • 3.3 policy($class)
      • 3.4 app_path($path = '')
      • 3.5 base_path($path = '')
      • 3.6 config_path($path = '')
      • 3.7 database_path($path = '')
      • 3.8 public_path($path = '')
      • 3.9 storage_path($path = '')
      • 3.10 abort($code, $message = '', array $headers = [])
      • 3.11 abort_if($boolean, $code, $message = '', array $headers)
      • 3.12 abort_unless($boolean, $code, $message = '', array $headers)
      • 3.13 Comparing abort, abort_if and abort_unless
      • 3.14 redirect($to = null, $status = 302, $headers = [], $secure = null)
      • 3.15 back($status = 302, $headers = [])
      • 3.16 response($content = '', $status = 200, array $headers = [])
      • 3.17 request($key = null, $default = null)
      • 3.18 view($view = null, $data = [], $mergeData = [])
      • 3.19 old($key = null, $default = null)
      • 3.20 event($event, $payload = [], $halt = false)
      • 3.21 dispatch($command)
      • 3.22 factory()
      • 3.23 method_field($method)
      • 3.24 validator(array $data = [], array $rules = [], array $messages = [], array $customAttributes = [])
    • 4. Configuration, Environment, Security and Logging Helper Functions
      • 4.1 bcrypt($value, $options = [])
      • 4.2 encrypt($value)
      • 4.3 decrypt($value)
      • 4.4 csrf_field()
      • 4.5 csrf_token()
      • 4.6 session($key = null, $default = null)
      • 4.7 cookie($name = null, $value = null, $minutes = 0, $path = null, $domain = null, $secure = false, $httpOnly = true)
      • 4.8 config($key = null, $default = null)
      • 4.9 env($key, $default = null)
      • 4.10 logger($message = null, $context = [])
      • 4.11 info($message, $context = [])
    • 5. URL Generation Helper Functions
      • 5.1 action($name, $parameters = [], $absolute = true)
      • 5.2 asset($path, $secure = null)
      • 5.3 secure_asset($path)
      • 5.4 url($path = null, $parameters = [], $secure = null)
      • 5.5 secure_url($path, $parameters = [])
      • 5.6 elixir($file, $buildDirectory = 'build')
      • 5.7 route($name, $parameters = [], $absolute = true, $route = null)
    • 6. Miscellaneous Helper Functions
      • 6.1 dd()
      • 6.2 data_get($target, $key, $default = null)
      • 6.3 data_set(&$target, $key, $value, $overwrite = true)
      • 6.4 data_fill(&$target, $key, $value)
      • 6.5 object_get($object, $key, $default = null)
      • 6.6 trait_uses_recursive($trait)
      • 6.7 value($value)
      • 6.8 with($object)
      • 6.9 windows_os
      • 6.10 tap($value, $callback)
    • 7. Overriding the Default Helper Functions
  • II Collections
    • 8. Basic Usage - Collections as Arrays
      • 8.1 Notes on Basic Usage
      • 8.2 Creating a New Collection Instance
    • 9. Collections: The Public API
      • 9.1 all
      • 9.2 toArray
      • 9.3 chunk($size, $preserveKeys = false)
      • 9.4 collapse
      • 9.5 contains($key, $value = null)
      • 9.6 count
      • 9.7 diff($items)
      • 9.8 diffKeys($items)
      • 9.9 each(callable $callback)
      • 9.10 filter(callable $callback = null)
      • 9.11 first(callable $callback = null, $default = null)
      • 9.12 flatten
      • 9.13 flip
      • 9.14 forPage($page, $perPage)
      • 9.15 forget($key)
      • 9.16 get($key, $default = null)
      • 9.17 groupBy($groupBy, $preserveKeys = false)
      • 9.18 has($key)
      • 9.19 implode($value, $glue = null)
      • 9.20 intersect($items)
      • 9.21 isEmpty
      • 9.22 jsonSerialize
      • 9.23 keys
      • 9.24 last(callable $callback = null, $default = null)
      • 9.25 lists($value, $key = null)
      • 9.26 map(callable $callback)
      • 9.27 max($key = null)
      • 9.28 merge($items)
      • 9.29 min($key = null)
      • 9.30 pluck($value, $key = null)
      • 9.31 pop
      • 9.32 prepend($value, $key = null)
      • 9.33 pull($key, $default = null)
      • 9.34 push($value)
      • 9.35 put($key, $value)
      • 9.36 random($amount = 1)
      • 9.37 reduce(callable $callback, $initial = null)
      • 9.38 reject($callback)
      • 9.39 reverse
      • 9.40 search($value, $strict = false)
      • 9.41 shift
      • 9.42 shuffle
      • 9.43 slice($offset, $length = null, $preserveKeys = false)
      • 9.44 sort(callable $callback = null)
      • 9.45 sortBy($callback, $options = SORT_REGULAR, $descending = false)
      • 9.46 sortByDesc($callback, $options = SORT_REGULAR)
      • 9.47 splice($offset, $length = null, $replacement = [])
      • 9.48 avg($key = null)
      • 9.49 average($key = null)
      • 9.50 sum($callback = null)
      • 9.51 take($limit)
      • 9.52 toJson($options = 0)
      • 9.53 __toString()
      • 9.54 transform(callable $callback)
      • 9.55 unique($key = null)
      • 9.56 values
      • 9.57 where($key, $operator, $value = null)
      • 9.58 whereLoose($key, $value)
      • 9.59 zip($items)
      • 9.60 combine($values)
      • 9.61 every($step, $offset = 0)
      • 9.62 except($keys)
      • 9.63 only($keys)
      • 9.64 keyBy($keyBy)
      • 9.65 whereIn($key, array $values, $strict = true)
      • 9.66 whereInLoose($key, array $values)
    • 10. Collections: The Static API
      • 10.1 make($items = [])
  • III Additional Helper Classes and Features
    • 11. Message Bags
      • 11.1 Illuminate\Contracts\Support\MessageProvider Interface
      • 11.2 View Error Bags
    • 12. Fluent
      • 12.1 Fluent Public API
    • 13. Macros
      • 13.1 Default Classes That Support Macros
      • 13.2 Creating a Macro
      • 13.3 Macroable Methods
    • 14. Facades
      • 14.1 Facade Aliases and Importing Facades
      • 14.2 Using Facades
      • 14.3 Creating Facades
      • 14.4 Facade Class Reference
      • 14.5 Resolving the Class Behind a Facade
  • Notes

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earnedover $13 millionwriting, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub