Book Image

Laravel 5 Essentials

By : Martin Bean
Book Image

Laravel 5 Essentials

By: Martin Bean

Overview of this book

Table of Contents (15 chapters)
Laravel 5 Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Saving data


Applications that display data are great, but they're not very interactive. The fun comes when you allow users to submit data, whether these users are trusted contributors adding content via a content management system or contributions from general users on a site like Wikipedia.

When you retrieve a record via Eloquent, you can access its properties as follows:

$cat = Cat::find(1);
print $cat->name;

We can update attribute values in the same manner:

$cat->name = 'Garfield';

This will set the value in the model instance, but we need to persist the change to the database. We do this by calling the save method afterwards:

$cat->name = 'Garfield';
$cat->save();

If you have a table with lots of columns, then it will become tiresome to assign each property manually like this. To this end, Eloquent allows you to fill models by passing an associative array with values, and the keys representing the column names. You can fill a model while either creating or updating it:

$data...