Courses

[NEW] React Native Mobile App with Laravel 12 API

Creating First CRUD

You're reading a FREE PREVIEW of a PREMIUM course.
Summary of this lesson:
- Create a new Laravel project with model and migration for Categories
- Build an API Resource Controller with necessary CRUD methods
- Install Sanctum for API authentication using Laravel's helper command
- Generate test data with factories and seeders for API testing

Link to the repository

[Only for premium members]

We will start our course from creating Laravel API for two Models: Categories and Transactions. In the following sections, we will start creating a React Native mobile application which would use this API.

This is how our mobile app screen will look like, after consuming API for Categories:

Next, we build a Laravel project for the API.

Notice: The Laravel part is identical to the API we created in earlier Laravel API + Flutter course, we will even re-use the same GitHub repository. So if you are coming from there and have the API created, you can skip to the next section with React Native lessons.


Creating a new Laravel Project

Let's start by creating a new Laravel project. Open your terminal and run the following command:

laravel new laravel-api-flutter-api-code

Notice: again, you see the word "flutter" here but this is because we're re-using the same Laravel API lessons from Flutter + Laravel course.

Then we will select None as our Starter kit:


Creating our First Model and Migration

Let's create our first model and migration for our categories. Run the following command:

php artisan make:model Category -m

Then we fill in our fields:

Migration

Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->nullable()->constrained();
$table->string('name');
$table->timestamps();
});

Then, we prepare our model:

app/Models/Category.php

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
 
class Category extends Model
{
use HasFactory;
 
protected $fillable = [
'user_id',
'name',
];
 
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
}

Creating a Controller

Let's create our API Resource Controller for our categories. Run the following command:

php artisan make:controller Api/CategoryController --resource --api --model=Category

This will create a new controller with all the necessary methods for our CRUD:

app/Http/Controllers/Api/CategoryController.php

use App\Http\Controllers\Controller;
use App\Models\Category;
use Illuminate\Http\Request;
 
class CategoryController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
//
}
 
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
//
}
 
/**
* Display the specified resource.
*/
public function show(Category $category)
{
//
}
 
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Category $category)
{
//
}
 
/**
* Remove the specified resource from storage.
*/
public function destroy(Category $category)
{
//
}
}

Returning First API Response

To keep this simple, we will add the...

The full lesson is only for Premium Members.
Want to access all 19 text lessons of this course? (91 min read)

You also get:

  • 77 courses
  • Premium tutorials
  • Access to repositories
  • Private Discord