select multiple tables in laravel code example
Example 1: laravel get data from multiple tables
<?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 get data from multiple tables
use App\Models\Post;
$comments = Post::find(1)->comments;
foreach ($comments as $comment) {
//
}