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.
models:
GameLeaderboard:
# ...
2
3
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class GameLeaderboard extends Model
{
// ...
}
2
3
4
5
6
7
8
9
10
<?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) {
// ...
});
}
// ...
}
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.
models:
Post:
title: string
excerpt: string nullable
body: longText
published_at: timestamp useCurrent nullable
user: belongsTo
2
3
4
5
6
7
class Post extends Model
{
use HasFactory;
protected $casts = ['published_at' => 'timestamp']
protected $fillable = [
'title',
'excerpt',
'body',
];
public function user()
{
return $this->belongsTo(User::class);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// database/factories/PostFactory.php
public function define()
{
// return [
// //
// ];
}
2
3
4
5
6
7
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();
});
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.
- Casting attributes to their appropriate type (dates, integers, arrays...)
- Hiding attributes from serialization, like passwords, tokens and secrets
- Fillable properties that are user-dependant
- Relationships methods and automatic pivot tables
- Migrations
- Factories
- Seeders
- Annotations for query methods, attributes and relations
Also, you can still configure the model you're declaring by using the following keys beneath it:
appends
: Appended attributescasts
: Castsfactory
: Factoryfillable
: Fillable attributeshidden
: Hidden attributesobservers
: Observersprimary
: Primary keyscopes
: Scopesseeder
: Seedertable
: Table nameuses
: Traitstype
: Model type
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:
- Primary key must be issued manually.
- Timestamps must be issued manually.
- Authenticatable Model must be changed manually.
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.
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
2
3
4
5
6
7
8
9
10
11
12
13
14