Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

Transactions CRUD

Premium
4 min read

In this lesson, we will repeat most of the steps we made for our Categories CRUD. But this time, we will create Transactions CRUD. Here's a quick overview:

  • Creating Models, Migrations
  • Creating Request classes
  • Creating TransactionController with CRUD methods
  • Creating TransactionResource
  • Adding API routes

So, let's dive into it.


Creating Models, Migrations

Let's start by creating our Model with migrations:

php artisan make:model Transaction -m

Then, let's update the migration file:

Migration

Schema::create('transactions', function (Blueprint $table) {
$table->id();
$table->foreignId('category_id')->constrained();
$table->foreignId('user_id')->nullable()->constrained();
$table->date('transaction_date');
$table->integer('amount');
$table->string('description');
$table->timestamps();
});

Our Model, which will have an amount attribute that will be cast to cents:

app/Models/Transaction.php

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
 
class Transaction extends Model
{
protected $fillable = [
'category_id',
'user_id',
'transaction_date',
'amount',
'description',
];
 
protected function casts()
{
return [
'transaction_date' => 'date',
];
}
 
protected function amount(): Attribute
{
return Attribute::make(
get: fn($value) => $value / 100,
set: fn($value) => $value * 100,
);
}
 
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
 
public function category(): BelongsTo
{
return $this->belongsTo(Category::class);
}
}

Because our Transactions are tied with Categories, we need to add a relationship to the categories Model:

app/Models/Category.php

use Illuminate\Database\Eloquent\Relations\HasMany;
 
// ...
 
public function transactions(): HasMany
{
return $this->hasMany(Transaction::class);
}

The Full Lesson is Only for Premium Members

Want to access all of our courses? (29 h 14 min)

You also get:

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

Already a member? Login here

Comments & Discussion

PV
Paul van Vulpen ✓ Link copied!

The "UpdateTransactionRequest" is missing inside the TransactionController code-block.

M
Modestas ✓ Link copied!

Updated! Thank you!

JP
Jason Page ✓ Link copied!

what are the benefits of having seperate Update and Store requests? vs having a TransactionRequest that we can use for both the storing and the updating? same applies to Category I guess. why have seperate?

M
Modestas ✓ Link copied!

Different request classes allow you to have different validation rules and fields for each action. Lets say you create a record with 5 mandatory fields, but have 10 more on edit that are optional.

Doing this in a single file is a nightmare in the long run :)