How to Database Relationships in Laravel10

 Type of Relationship in Database Laravel 

  1. One-to-One - One Record in the table is associated with One. For example a customer and card ID number
  2. One to Many - One record in the table is associated with multiple records. 
  3. Many to Many - Multiple records in the table are associated with multiple records.

1. One-to-One Using in-Class Model: hasOne() 


Example

                $this->hasOne(Product:::class);


2. One to Many using in Class Model:  hasMany() Method


Example 


                $this->hasMany(Product::class);


3. Many to Many Using in-Class Model:  hasOne()  latestOfMany(), oldestOfMany() and ofMany()

Example 



                $this->hasOne(Product::class)->latestOfMany();
                

                $this->hasOne(Product::class->oldestOfMany();




ความคิดเห็น