Skip to main content

DB Model, New Vue Page and Menu Item

Premium
5 min read

Let's try to create a simple CRUD for Tasks with two fields (for now): name and is_completed.

In this lesson, we'll manage the Model/Migration, Routes, and Controllers and add a navigation link in the top menu.


Preparing the Database

First, we create the DB structure with factories to create some fake records:

php artisan make:model Task -mf

The table structure is in Migration.

database/migrations/xxxx_create_tasks_table.php:

public function up(): void
{
Schema::create('tasks', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->boolean('is_completed')->default(false);
$table->timestamps();
});
}

In the Model, we just make the fields fillable and cast is_completed to boolean.

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
 
class Task extends Model
{
use HasFactory;
 
protected $fillable = [
'name',
'is_completed'
];
 
protected function casts(): array
{
return [
'is_completed' => 'boolean'
];
}
}

Then, the Factory with the...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (30 h 09 min)

You also get:

55 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

GA
GIUSEPPE ALESSANDRO DE BLASIO ✓ Link copied!

If you don't use the show method in TaskController maybe it's better to use Route::resource('tasks', TaskController::class)->except('show') in web.php;

NG
Nweke Godswill ✓ Link copied!

Alright bro. Whats the advantage though?

M
Modestas ✓ Link copied!

You dont register routes you will never use, but in any case they are 404

NG
Nweke Godswill ✓ Link copied!

Alright, thanks

M
Milan ✓ Link copied!

It seems as in new version instead of 'url' we have 'href':

{
title: 'Tasks',
url: '/tasks',
icon: BriefcaseIcon,
},
 
{
title: 'Tasks',
href: '/tasks',
icon: BriefcaseIcon,
},
M
Modestas ✓ Link copied!

Good catch, will add to our to-do list to update the course. Thanks!

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.