Support the ongoing development of Laravel.io →
posted 4 years ago
Last updated 3 years ago.
0

In your migration, change your foriegn key line with this :

$table->foreign('category_id')->references('id')->on('categories');

In your Category model : (if you are calling the other model Object, so put objects in the function otherwise put the name of your model instead in plural)

public function objects(){ return $this->hasMany(Object::class); }

In your Object model :

public function category(){ return $this->belongsTo(Category::class); }

Last updated 4 years ago.
1

Thanks for your help !

1

Sign in to participate in this thread!

Native PHP

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.