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); }}
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners