Defining Relationships: One To Many

Is used to define relationships where a single model owns any amount of other models.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    /**
     * Get the comments for the blog post.
     */
    public function comments()
    {
        return $this->hasMany('App\Comment');
    }
}
Return $this->hasMany('App\Comment', 'foreign_key');

return $this->hasMany('App\Comment', 'foreign_key', 'local_key');
$comments = App\Post::find(1)->comments;

foreach ($comments as $comment) {
    //
}
$comment = App\Post::find(1)->comments()->where('title', 'foo')->first();

Defining Relationships: One To Many — Structure map

Clickable & Draggable!

Defining Relationships: One To Many — Related pages: