Hi Fellas, In this blog, we'll walk through Creating relationships between models ( Ex Library has_many Books ).
STEP ⃣: Let's create a new laravel project using the laravel new demo here demo is my project name
Add the below code in the public function up() method in the library and book the migration file // code to be added to the library migration file Schema::create('libraries', function (Blueprint $table) { $table->id(); $table->timestamps(); $table->string('name'); $table->string('librarian_name'); $table->datetime('opening_time'); $table->datetime('closing_time'); }); // code to be added to the book migration file
STEP ⃣: Let us define the has_many relationship in the models ( Ex: Library has many books ) class Library extends Model { use HasFactory; public function books() { return $this->hasMany(Book::class); }}