Skip to main content

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

Read more here

List inside of List: Multi-Level Data

Premium
4:13

In this lesson, we will discuss relationships between tables and between Eloquent Models and how to return them from the API Resources.


First, we must create a new Model to have a relation with a category.

php artisan make:model Product -mf

database/migration/xxx_create_products_table.php:

public function up(): void
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->foreignId('category_id')->constrained();
$table->string('name');
$table->text('description');
$table->integer('price');
$table->timestamps();
});
}

app/Models/Product.php:

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

database/factories/ProductFactory.php:

use App\Models\Category;
 
class ProductFactory extends Factory
{
public function definition(): array
{
return [
'name' => fake()->word(),
'category_id' => Category::inRandomOrder()->first()->id,
'description' => fake()->paragraph(),
'price' => rand(1000, 99999),
];
}
}

database/seeders/DatabaseSeeder.php:

 
use App\Models\Product;
 
class DatabaseSeeder extends Seeder
{
public function run(): void
{
Category::factory(10)->create();
Product::factory(20)->create();
}
}

And run the migrations with the seed.

php artisan migrate:fresh --seed

Next, the Controller, Resource, and Route.

php artisan make:controller Api/ProductController
php artisan make:resource ProductResource

routes/api.php:

Route::get('/user', function (Request $request) {
return $request->user();
})->middleware(Authenticate::using('sanctum'));
 
Route::get('categories', [\App\Http\Controllers\Api\CategoryController::class, 'index']);
Route::get('categories/{category}', [\App\Http\Controllers\Api\CategoryController::class, 'show']);
 
Route::get('products', [\App\Http\Controllers\Api\ProductController::class, 'index']);

In the Controller, we can return the product...

The Full Lesson is Only for Premium Members

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

FF
Francisco Ferreira Roque Júnior ✓ Link copied!

if some one have problems at the database/factories/ProductFactory.php step. just remember to also "include Category model on that" -> use App\Models\Category;

M
Modestas ✓ Link copied!

Sorry about that, we have updated the lesson!

FF
Francisco Ferreira Roque Júnior ✓ Link copied!

Great class guys, thank you very much.

I didn't know it was possible to use resources inside resources.

LS
Luis Scura ✓ Link copied!

If we are showing the category info with CategoryResource, does it makes sense to display the "'category_id' => $this->category_id," itself? Is it usually good practice to display it too?