laravel relationships
Any web project may have a relationship between database tables. For an example of a relationship between a database, a shop product may have many reviews or an Invoice could be related to the user who makes it.
laravel Eloquent has a lot of methods that make working with laravel relationships smoothly
laravel relationships support different types of relationships:
- One To One
- One To Many
- Many To Many
Define One to One relationship
think of this relationship like a user table and user profile image, one user has one profile image this relationship is a very basic relation.
in user/image example a User model may be associated with one Photo. we can define this relationship by place a photo method on the User model.
Then we can access the photo method and return its result:
Note: you can change the foreign key and the references column
return $this->hasOne('App\Photo', 'foreign_key', 'local_key');
One to One Inverse Relationship
now you can define the inverse relationship between user and photo to access the user data by there photo using the belongsTo
method
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Photo extends Model
{
/**
* Get the user that owns the Photo .
*/
public function user()
{
return $this->belongsTo('App\User');
}
}
Define One to Many relationships
One to Many relationships is the same relation between product and photo.
a single product can have on or more photo and the photo belong to a single product.
now lets make our one to many relation between product and photo
in the product Eloquent model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
/**
* Get the photo for the product
*/
public function comments()
{
return $this->hasMany('App\Photo');
}
}
Note: you can change the foreign key and the references column
return $this->hasMany('App\Photo', 'foreign_key', 'local_key');
One to Many Inverse Relationship
Now let's define the inverse of a hasMany
the relationship that allows us to get the product from the photo.
you can use the belongsTo
method on the photo Eloquent module
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Photo extends Model
{
/**
* Get the Ptoduct that owns the Photo.
*/
public function product()
{
return $this->belongsTo('App\Product');
}
}
Define Many to Many relationships
Many to Many relationships is considered one of the simplest relationships. Understanding it represents the participatory relationship between something like the relationship between a product and its category, where for each category there are more than one product and a single product may belong to more than one category.
In order to properly formulate it, we need 3 tables in the database one for a Product and one for the category and last one for the relation between product and category
product
id - integer
name - string
category
id - integer
name - string
category_product
product_id - integer
category_id - integer
Note. Remember to name the relation table "category_product" depending on that consists of the model names of the two main tables category, product, and that names are placed in alphabetical order and separated by a ( _ ).
Let's define the category
method on our product
model. to define the Many-to-many relationships we can use the belongsToMany
method. For example
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
/**
* The category that belong to the product.
*/
public function categorys()
{
return $this->belongsToMany('App\Category');
}
}