laravel eloquent get model with relations
<?php
//For example, imagine a blog application in which a User model has many associated Post models:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* Get all of the posts for the user.
*/
public function posts()
{
return $this->hasMany(Post::class);
}
}
//You may query the posts relationship and add additional constraints to the relationship like so:
use App\Models\User;
$user = User::find(1);
$user->posts()->where('active', 1)->get();