Example 5-18. Operations achievable with the simplest Eloquent model
// In a controllerpublicfunctionsave(Request $request){// Create and save a new contact from user input $contact =newContact(); $contact->first_name = $request->input('first_name'); $contact->last_name = $request->input('last_name'); $contact->email = $request->input('email'); $contact->save();returnredirect('contacts');}publicfunctionshow($contactId){// Return a JSON representation of a contact based on a URL segment;// if the contact doesn't exist, throw an exceptionreturnContact::findOrFail($contactId);}publicfunctionvips(){// Unnecessarily complex example, but still possible with basic Eloquent// class; adds a "formalName" property to every VIP entryreturnContact::where('vip',true)->get()->map(function ($contact) { $contact->formalName ="The exalted {$contact->first_name} of the {$contact->last_name}s";return $contact; });}