what is a eloquent relationship laravel code example
Example 1: laravel eloquent relationship
<?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(Comment::class);
}
}
Example 2: laravel relationship
use App\Models\Post;
$comments = Post::find(1)->comments;
foreach ($comments as $comment) {
//
}