Book Image

Getting Started with Laravel 4

By : Raphaël Saunier
Book Image

Getting Started with Laravel 4

By: Raphaël Saunier

Overview of this book

<p>PHP powers many of the largest websites on the planet. Yet, even though it was specifically created for the Web, its shortcomings never cease to frustrate developers. This is where a tool like Laravel comes in. Rather than reinventing the wheel, Laravel reuses tried and tested components and principles and bundles them to form a cohesive whole and makes PHP development enjoyable again.</p> <p>Getting Started with Laravel 4 is a practical and concise introduction to the Laravel PHP framework. It covers its fundamental concepts and presents the many features that will boost your productivity when developing web applications. After introducing the key concepts and installing Composer, you will build a CRUD application and add more features to it in each successive chapter.</p> <p>This book introduces you to a different and more enjoyable way of writing PHP applications. You will start by learning about the key principles and the same development practices that Laravel encourages. Then, in subsequent chapters, you will create and successively add more features to a web application.</p> <p>You will learn how to use the arsenal of tools at your disposal and probably pick up some useful techniques along the way. Indeed, everything you will learn in this book is highly transferrable and applicable to other MVC frameworks. Laravel's routing mechanism, templating language, and object-relational mapper will have no more secrets for you. You will learn how to authenticate users, write tests, and create command line utilities that interact with your application with disconcerting ease. In addition to this, you will probably be surprised by the simplicity and expressiveness of your code.</p>
Table of Contents (15 chapters)
Getting Started with Laravel 4
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Array helpers


Arrays are the bread and butter of any web application that deals with data. PHP already offers nearly 80 functions to perform various operations on arrays, and Laravel complements them with a handful of practical functions that are inspired by certain functions found in Python and Ruby.

Note

Several of Laravel's classes, including Eloquent collections, implement the PHP ArrayAccess interface. This means that you can use them like a normal array in your code and, for instance, iterate over the items in a foreach loop or use them with the array functions described here.

Most of the functions support a dot notation to refer to nested values, which is similar to JavaScript objects. For example, rather than writing $arr['foo']['bar']['baz'], you can use the array_get helper and write array_get($arr, 'foo.bar.baz');.

In the following usage examples, we will use three dummy arrays and assume that they are reset for each example:

$associative =  array(
  "foo" => 1,
  "bar" => 2,
);
$multidimensional = array(
  "foo" => array( 
      "bar" => 123,
  ),
);
$list_key_values = array(
  array("foo" => "bar"), 
  array("foo" => "baz"),
);

The usage examples of array helpers

We will now have a look at how we can use Laravel's array helper functions to extract and manipulate the values of those arrays:

  • To retrieve a value with a fallback value if the key does not exist, we use the array_get function:

    array_get($multidimensional, 'foo.bar', 'default');
    // Returns 123
  • To remove a value from an array using the dot notation, we use the array_forget function:

    array_forget($multidimensional, 'foo.bar');
    // $multidimensional == array( 'foo' => array() );
  • To remove a value from an array and return it, we use the array_pull function:

    array_pull($multidimensional, 'foo.bar');
    // Returns 123 and removes the value from the array
  • To set a nested value using the dot notation, we use the array_set function:

    array_set($multidimensional, 'foo.baz', '456');
    // $multidimensional == array( 'foo' => array( 'bar' => 123, 'baz' => '456' ) );
  • To flatten a multidimensional associative array, we use the array_dot function:

    array_dot($multidimensional);
    // Returns array( 'foo.bar' => 123 );
    array_dot($list_key_values);
    // Returns array( '0.foo' => 'bar', '1.foo' => 'baz' );
  • To return all of the the keys and their values from the array except for the ones that are specified, we use the array_except function:

    array_except($associative, array('foo',));
    // Returns array('bar' => 2);
  • To only extract some keys from an array, we use the array_only function:

    array_only($associative, array('bar'));
    // Returns array('bar' => 2);
  • To return a flattened array containing all of the the nested values (the keys are dropped), we use the array_fetch function:

    array_fetch($list_key_values, 'foo');
    // Returns array('bar', 'baz')
  • To iterate over the array and return the first value for which the closure returns true, we use the array_first function:

    array_first($associative, function($key, $value){
       return $key == "foo";
    });
    // Returns 1
  • To generate a one-dimensional array containing only the values that are found in a multidimensional array, we use the array_flatten function:

    array_flatten($multidimensional);
    // Returns array(123)
  • To extract an array of values from a list of key-value pairs, we use the array_pluck function:

    array_pluck($list_key_values, 'foo');
    // Returns array('bar', 'baz');
  • To get the first or last item of an array (this also works with the values returned by functions), we use the head and last functions:

    head($array); // Aliases to reset($array)
    last($array); // Aliases to end($array)