Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Implementing relationships in Eloquent models

Thursday, December 10th, 2015 by Servage

data-modelFollowing up on the recent article about Laravel models, this article will show how you can implement model relationships with Eloquent. Relationships between objects and database models are basically the foundation or blueprint on how models relate to each other. This has an impact on the way you can work with data. Defining model relationships in Eloquent provides you with a lot of functionality right out of the box.

Relationship types

Basic understanding of relationship types is required to define the correct relationships between models. Let us say you have an online store, so everything you have evolves around your products (i.e. the product model). Your products can have zero, one or multiple images, while they can be tagged with zero, one or multiple tags. This scenario illustrates some of the most common relationship types.

  • Product has many images (can also have none or one).
  • Image belongs to product (but cannot belong to multiple products).
  • Product can have many tags, and tags can belong to multiple products.

The logic rules above will now be implemented into the Eloquent model classes, so Laravel knows what to do about them.

Implementing relationships

First, we define the relationships in the product model.

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
  public function images()
  {
    return $this->hasMany('App\Image');
  }

  public function tags()
  {
    return $this->belongsToMany('App\Tag');
  }
}
?>

Next, we implement the relationship in the image model. This requires that the images table has a foreign key product_id, to indicate which product each image belongs to.

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class Image extends Model
{
  public function products()
  {
    return $this->belongsTo('App\Product');
  }
}
?>

Last, we define the relationship in the tag model. This requires a pivot table that connects the products with the tags. It must be an intermediary table because neither products nor tags belong to only one counterpart, hence the need for a table in between.

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class Tag extends Model
{
  public function products()
  {
    return $this->belongsToMany('App\Product');
  }
}
?>

Working with related data

After defining the relationships you can retrieve data based on the relationships.

$product = App\Product::find(1);

foreach ($product->images as $image)
{
  // Do something with each image...
}

The above illustrates how you can define relationships and touches upon how easily you can use the related data. This is very powerful because it makes it really easy to connect your data and use it in a meaningful way.

Implementing relationships in Eloquent models, 5.0 out of 5 based on 4 ratings
Categories: Guides & Tutorials

Keywords: ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.