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

Easier date and time handling with Carbon


Laravel bundles Carbon (https://github.com/briannesbitt/Carbon), which extends and augments PHP's native DateTime object with more expressive methods. Laravel uses it mainly to provide more expressive methods on the date and time properties (created_at, edited_at, and deleted_at) of an Eloquent object. However, since the library is already there, it would be a shame not to use it elsewhere in the code of your application.

Instantiating Carbon objects

Carbon objects are meant to be instantiated like normal DateTime objects. They do, however, support a handful of more expressive methods.

  • Carbon objects can be instantiated using the default constructor that will use the current date and time:

    • $now = new Carbon();

  • They can be instantiated using the current date and time in a given timezone:

    • $jetzt = new Carbon('Europe/Berlin');

  • They can be instantiated using expressive methods:

    • $yesterday = Carbon::yesterday();

    • $demain = Carbon::tomorrow("Europe/Paris");

  • They can be instantiated using exact parameters:

    • Carbon::createFromDate($year, $month, $day, $tz);

    • Carbon::createFromTime($hour, $minute, $second, $tz);

    • Carbon::create($year, $month, $day, $hour, $minute, $second, $tz);

Outputting user-friendly timestamps

We can generate human-readable relative timestamps such as 5 minutes ago, last week, or in a year with the diffForHumans() method:

$post = Post::find(123);
echo $post->created_at->diffForHumans(); 

Boolean methods

Carbon also provides a handful of simple and expressive methods that will come in handy in your controllers and views:

  • $date->isWeekday();

  • $date->isWeekend();

  • $date->isYesterday();

  • $date->isToday();

  • $date->isTomorrow();

  • $date->isFuture();

  • $date->isPast();

  • $date->isLeapYear();

Carbon for Eloquent DateTime properties

To be able to call Carbon's methods on attributes stored as DATE or DATETIME types in the database, you need to list them in a getDates() method in the model:

class Post extends Eloquent {
  // ...
  public function getDates() {
    return array('created_at', 'edited_at', 'published_at',); 
  }
}