Skip to content
On this page

Models

Start creating a Model under the models key by just using the name of the model. The table for it will be inferred automatically using Laravel conventions.

For example, GameLeaderboard will make a table called game_leaderboards, without needing to set the name or migration somewhere else.

yaml
models:
  GameLeaderboard:
    # ...
1
2
3
php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class GameLeaderboard extends Model
{
    // ...
}
1
2
3
4
5
6
7
8
9
10
php
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('game_leaderboards', function (Blueprint $table) {
           // ...
        });
    }
    
    // ...
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

There are two ways to create a model: the quick way and the custom way. Let's start with the Quick Model, which pretty much straightforward to declare.

Quick model

Quick models are the preferred way to create a model that follows Laravel conventions. Just issue the columns directly below the model. The key will be the column name, the value will be the type.

When creating a Quick Model, Larawiz will automatically add timestamps and the id as primary key.

yaml
models:
  Post:
    title: string
    excerpt: string nullable
    body: longText
    published_at: timestamp useCurrent nullable
    user: belongsTo
1
2
3
4
5
6
7
php
class Post extends Model
{
    use HasFactory;
    
    protected $casts = ['published_at' => 'timestamp']

    protected $fillable = [
        'title',
        'excerpt',
        'body',
    ];

    public function user()
    {
        return $this->belongsTo(User::class);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
php
// database/factories/PostFactory.php
public function define()
{
//    return [
//      //  
//    ];
}
1
2
3
4
5
6
7
php
Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->string('title');
    $table->string('excerpt')->nullable();
    $table->longText('body');
    $table->timestamp('published_at')->useCurrent()->nullable();
    $table->foreignIdFor(User::class);
    $table->timestamps();
});
1
2
3
4
5
6
7
8
9

Chores and configuration

When creating a model, the following logic will always be automated, which are mostly chores that take a lot of time once you pile them up.

Also, you can still configure the model you're declaring by using the following keys beneath it:

You can have your columns back

If you want to have a column name that collides with these reserved keys, you can separate the columns using a Custom Model.

Custom Model

Custom Models gives you full control on the Model itself. Just explicitly tell which columns you want to create by separating the columns from the rest of the Model configuration. Since Larawiz hands-off the columns to you, this comes with some small drawbacks:

For example, let's say we want the Worker model to have a column called factory, where you may put the factory name he is assigned to. Since this collides with the factory key, we can separate the columns through the columns key.

yaml
namespace: App\Models

models:
  Worker:
    columns:
      id: ~ # Add the ID manually
      name: string
      is_busy: boolean default:false
      factory: string
      timestamps: ~ # Add the timestamps manually
      softDeletes: ~
    factory:
      - withoutFactory
      - available
1
2
3
4
5
6
7
8
9
10
11
12
13
14