eloquent has many code example
Example 1: laravel has many
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
/**
* Get the comments for the blog post.
*/
public function comments()
{
return $this->hasMany('App\Models\Comment');
}
}
Example 2: laravel relation has many
// Post model
public function comments()
{
return $this->hasMany(Comment::class);
}
// Post controller
$comments = Post::find(1)->comments;