Black Friday: coupon FRIDAY24 for 40% off Yearly/Lifetime membership! Read more here
Courses

Laravel 11 Eloquent: Expert Level

Model Properties: Tables, Keys, Increments, Pages and Dates

In an Eloquent Model, you can provide a lot of properties. With most of them, you would override their default values coming from the framework internals.


Customize DB Table Name

The most well-known fact is that you can override the table name. You can specify that your Task Model should work not with the tasks table by default but with the project_tasks table, instead.

class Task extends Model
{
protected $table = 'project_tasks';
}

Customize Primary Key

The next thing you can override is the primary key. By default, the primary key is id. But you may change it in the migration to, for example, task_id.

public function up(): void
{
Schema::create('tasks', function (Blueprint $table) {
$table->id('task_id');
$table->timestamps();
});
}

Then, in the Model, you must provide the $primaryKey.

class Task extends Model
{
protected $table = 'project_tasks';
 
protected $primaryKey = 'task_id';
}

And then, everywhere, Eloquent will try to search by primary key; for example, in Task::find() or similar operations, it would use this primary key.

That primary key may still be auto-incremented. It's just about the name of the field.


Customize Auto-Increments

What if you don't want it to be auto increment? Maybe you will set it up yourself manually with some logic. Then, you will set the $incrementing to false.

class Task extends Model
{
protected $table = 'project_tasks';
 
protected $primaryKey = 'task_id';
 
public $incrementing = false;
}

And then, in your migration, probably, you should have unsignedBigInteger() without auto-increment, instead of id().

Or, if you want to use UUIDs or ULIDs specifically for that there is a trait.

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
 
class Task extends Model
{
use HasUuids;
 
protected $table = 'project_tasks';
 
protected $primaryKey = 'task_id';
 
public $incrementing = false;
}

Then, in the Migration, instead of id(), you would use uuid().


Customize Pagination

The next thing you can provide or override is a property called per page, which is, by default, 15. It would be used in all of your pagination requests. So, for example, we can set it to 10.

class Task extends Model
{
protected $table = 'project_tasks';
 
protected $primaryKey = 'task_id';
 
public $incrementing = false;
 
protected $perPage = 10;
}

In the Controller for the query, we would call paginate().

use App\Models\Task;
 
class HomeController extends Controller
{
public function index()
{
Task::paginate();
}
}

When you can specify it in the Model, then for any paginated requests anywhere in your Controllers or anywhere the Task Model will be paginated 10 per page.


Customize Timestamps: Don't Use Them

And a few things about timestamps. By default, in your migration, you have timestamps that are created_at and updated_at. You can refuse to use those.

You may have some other logic for timestamps or want to use something other than that. In your Model, you can set $timestamps to false.

class Task extends Model
{
protected $table = 'project_tasks';
 
protected $primaryKey = 'task_id';
 
public $incrementing = false;
 
protected $perPage = 10;
 
public $timestamps = false;
}

Then you don't need the timestamps() in the Migration.

public function up(): void
{
Schema::create('tasks', function (Blueprint $table) {
$table->id('task_id');
$table->timestamps();
});
}

Then Eloquent will not try to automatically fill in created_at and updated_at because it will look at the settings.


Customize Timestamps: Rename Them

In another case, the names of your timestamps are different. For example, if you have a database from an older project, not even Laravel, maybe you have not created the created_at and updated_at, but time_created and time_updated. Then, you can override the constants.

class Task extends Model
{
protected $table = 'project_tasks';
 
protected $primaryKey = 'task_id';
 
public $incrementing = false;
 
protected $perPage = 10;
 
public $timestamps = false;
 
const CREATED_AT = 'time_created';
 
const UPDATED_AT = 'time_updated';
}

And, of course, then, in the migration, you don't use timestamps(). You create those fields manually.


Bonus: Quickly Check Table/Model Properties

If you made custom changes to your Model or DB Table, you may have forgotten about some of them. Laravel can help you to perform a quick check.

It has two artisan commands showing information about the database and specific database tables.

You can use the db:show artisan command to see information about database and tables. This command will show you general information about your database and all your migrated tables with their sizes.

Another artisan command is model:show, which shows information about a provided table. This command will show all the table's fields, relationships, and observers.

No comments or questions yet...