0PricingLogin
PHP Academy · Lesson

CRUD with Eloquent

Create, read, update, and delete records using Eloquent methods.

Create — save()

Create a model instance, set attributes, and call save().

<?php
$post = new Post();
$post->title = "Hello World";
$post->body  = "My first post";
$post->save(); // INSERT INTO posts ...

Create — create()

Model::create() inserts a row in one line using mass assignment. Requires $fillable to be set.

<?php
$post = Post::create([
    "title" => "Hello World",
    "body"  => "My first post",
    "user_id" => auth()->id(),
]);

All lessons in this course

  1. Eloquent Models and Migrations
  2. CRUD with Eloquent
  3. Eloquent Relationships
  4. Eager Loading and Query Scopes
← Back to PHP Academy