Laravel Eloquent: Inserting & Updating Models

Inserts

<?php

namespace App\Http\Controllers;

use App\Flight;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class FlightController extends Controller
{
    /**
     * Create a new flight instance.
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(Request $request)
    {
        // Validate the request...

        $flight = new Flight;

        $flight->name = $request->name;

        $flight->save();
    }
}

Updates

$flight = App\Flight::find(1);

$flight->name = 'New Flight Name';

$flight->save();

Mass Assignment

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Flight extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name'];
}
$flight = App\Flight::create(['name' => 'Flight 10']);
$flight->fill(['name' => 'Flight 22']);

Other Creation Methods

Laravel Eloquent: Inserting & Updating Models — Structure map

Clickable & Draggable!

Laravel Eloquent: Inserting & Updating Models — Related pages: