Laravel: Models

Carlos Costa

Model is the most important part of Laravel. It is a class that extends the base Model class and contains the attributes and relationships of the model.

Ok, let’s create a simple flow to create, setup and test an model:

  • Create a model
  • Migrations
  • Model config
  • Factory
  • Test

Create a model


Let’s create a model called Car with migration, factories and tests.

php artisan make:model Car -mf --pest

Migrations


Let’s add some properties to our model:

Schema::create('cars', function (Blueprint $table) {
    Schema::create('cars', function (Blueprint $table) {
        $table->uuid('id')->primary();
        $table->timestamps();

        $table->string('name');
        $table->string('model');
        $table->integer('year');
        $table->string('color');
        $table->softDeletes();
    });
});

$table->softDeletes() will add a deleted_at column to our table.

See more: Soft Deletes

Model config


Our model will have factory, will use UUIDs and will be soft deleted.

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\SoftDeletes;

class Car extends Model
{
    use HasFactory;
    use HasUuids;
    use SoftDeletes;

    protected $fillable = [
        'name',
        'model',
        'year',
        'color',
    ];
}

Factory


Defining our factory:

return [
    'name' => $this->faker->name(),
    'model' => $this->faker->word(),
    'year' => $this->faker->year(),
    'color' => $this->faker->colorName(),
];

Tests


To finally let’s apply some tests to our models.

use Illuminate\Foundation\Testing\RefreshDatabase;
use App\Models\Car;

uses(RefreshDatabase::class);

test('Car: create', function () {
    $car = Car::factory()->create();

    $this->assertTrue($car->exists);
    $this->assertNotNull($car->id);
});

test('Car: update', function () {
    $car = Car::factory()->create();

    $car->name = 'Tesla';
    $car->model = 'Model X';
    $car->year = 2023;
    $car->color = 'Blue';

    $car->save();

    $this->assertEquals('Tesla', $car->name);
    $this->assertEquals('Model X', $car->model);
    $this->assertEquals(2023, $car->year);
    $this->assertEquals('Blue', $car->color);
});

test('Car: soft delete', function () {
    [$firstCar, $secondCar, $thirdCar] = Car::factory(3)->create();

    $firstCar->delete();

    $this->assertNotNull($firstCar->deleted_at);
    $this->assertNull($secondCar->deleted_at);
    $this->assertNull($thirdCar->deleted_at);

    $cars = Car::all();
    $this->assertEquals(2, $cars->count());

    $deletedCars = Car::onlyTrashed()->get();
    $this->assertEquals(1, $deletedCars->count());

    $firstCar->restore();

    $cars = Car::all();
    $this->assertEquals(3, $cars->count());
});

test('Car: hard delete', function () {
    [$firstCar] = Car::factory(3)->create();

    $firstCar->forceDelete();

    $cars = Car::all();
    $this->assertEquals(2, $cars->count());

    $deletedCars = Car::onlyTrashed()->get();
    $this->assertEquals(0, $deletedCars->count());
});

Reference