Skip to main content

Customer Tasks and Calendar View

Premium
10 min read

Now that we have Employees - they usually have to perform specific Tasks with our Customers. For example, they might need to make a phone call to them or send over some documents. For that, we can build a Task system with a calendar view like this:

In this lesson, we will do the following:

  • Create Task Model and Database
  • Add the Create Task button to the Customer list
  • Add Task list to the Customer page (view page)
  • Add Task Resource with Tabs
  • Add a Calendar page for Tasks

Create Task Model and Database

Let's start with our Models and Database structure:

Migration

use App\Models\Customer;
use App\Models\User;
 
// ...
 
Schema::create('tasks', function (Blueprint $table) {
$table->id();
$table->foreignIdFor(Customer::class)->constrained();
$table->foreignIdFor(User::class)->nullable()->constrained();
$table->text('description');
$table->date('due_date')->nullable();
$table->boolean('is_completed')->default(false);
$table->timestamps();
});

Then, we can fill our Model:

app/Models/Task.php

use Illuminate\Database\Eloquent\Relations\BelongsTo;
 
class Task extends Model
{
protected $fillable = [
'customer_id',
'user_id',
'description',
'due_date',
'is_completed',
];
 
protected $casts = [
'due_date' => 'date',
'is_completed' => 'boolean',
];
 
public function customer(): BelongsTo
{
return $this->belongsTo(Customer::class);
}
 
public function employee(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id');
}
}

That's it. We have our base structure for the Task Model and Database.


Add Create Task Button to the Customer list

Next, we want to add a button to...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (30 h 01 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

RA
Richard A. Hoyle ✓ Link copied!

Question how do we go about changing the time for the task the only thing coming up is 12a in front of every task?

M
Modestas ✓ Link copied!

If you need the time - you need to have dateTime column on your tasks and not just date. That is indeed a limitation on the calendar integration - it sets 12am as default date if none is provided.

S
Sjoerd24 ✓ Link copied!

If you add this: ->allDay(true)

to the EventData::make() you will render them without time, so it won't display 12a or 00 in front of the events.

D
devcodigo ✓ Link copied!

Helo, thanks for wonderful course. I hit a problem I am not able to solve. In CustomerResource Infolist on Tabs with Tasks there is ->suffixAction at is_completed TextEntry. If I use the action with the closure: ->action(function (Task $record) { $record->is_completed = true; $record->save(); i got an axception, that the model Customer instead of model Task was provided to the closure. It seems it injects the $record of the main record of the infolist = Customer and not the actual record of the repeatable entry.

S
SOIXT ✓ Link copied!

Hi guys,

I just found out that something strange happens with table action form.

When we select employee in the form, somehow it automatically updates the $customer employee_id to selected user_id...

->action(function (Customer $customer, array $data) { dd($customer, Customer::find($customer->id)); })

It is happening when we use ->relationship('employee', 'name'), to fix it we can change it to ->options(User::all()->pluck('name', 'id')) but it is strange it is doing that in the background.

M
Modestas ✓ Link copied!

Hi, I'm not sure I fully understood the issue you are facing here.

Does this happen on an action when you have more than one row selected? What about just a single row selected, is it working as expected?

S
SOIXT ✓ Link copied!

It happens on this form:

Tables\Actions\Action::make('Add Task') ->icon('heroicon-s-clipboard-document') ->form([ Forms\Components\RichEditor::make('description') ->required(), Forms\Components\Select::make('user_id') ->preload() ->searchable() ->relationship('employee', 'name'), Forms\Components\DatePicker::make('due_date') ->native(false),

])
->action(function (Customer $customer, array $data) {
$customer->tasks()->create($data);
 
Notification::make()
->title('Task created successfully')
->success()
->send();
})

Its per record

M
Modestas ✓ Link copied!

Hmm, I have not experienced this. Could you dump the $data using dd($data) and see what it returns? As it shouldn't set the employee id, unless you have an employee ID on the form...

Or maybe something happens if you have more than 1 record selected and try to create a task - not sure

S
SOIXT ✓ Link copied!

Its not problem in the data. Forms\Components\Select::make('user_id') ->preload() ->searchable() ->relationship('employee', 'name')

This like auto-populates employee_id when it is using relationship, if you dd($customer) you can see it changes it when the select changes..

M
Modestas ✓ Link copied!

This is weird. I don't see such behaviour, and I don't remember it being like this...

KB
Kurt Bauman ✓ Link copied!

I new and learing. If I use UTC time on the app and have a timezone field for my user, how can I have the text column adjust from UTC to customer's time zone? This is probably easy to do but i've searched and couldn't find anything specific to filament. Right now I have a text column that shows the timestamp from my database, I really need to recalucate the proper time before displaying. Any help will be greatly appreciated. Thank you.

SI
Solomon Iroegbu ✓ Link copied!

Thank you very much. Please when i add this to CustomerResource below i'm encountering an error.

Tables\Actions\Action::make('Add Task') ->icon('heroicon-s-clipboard-document') ->form([ Forms\Components\RichEditor::make('description') ->required(), Forms\Components\Select::make('user_id') ->preload() ->searchable() ->relationship('employee', 'name'), Forms\Components\DatePicker::make('due_date') ->native(false),

])
->action(function (Customer $customer, array $data) {
$customer->tasks()->create($data);
 
Notification::make()
->title('Task created successfully')
->success()
->send();
})
M
Modestas ✓ Link copied!

What is the error?

SI
Solomon Iroegbu ✓ Link copied!

I've fixed that. thank you.

SI
Solomon Iroegbu ✓ Link copied!

O lecture fifteen. Im havig difficultyu in creating a Calendar widget. It shows

Unable to find component: [App\Livewire\TaskCalendarWidget]

after following your procedures. Thanks for quick response.

M
Modestas ✓ Link copied!

It seems that you don't have the widget class declared correctly. Please check the file that's below the blade code snippet

SI
Solomon Iroegbu ✓ Link copied!

Resolved. Thank you so much.

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.