Did you know that we can always load relationships without calling it from our controller or from our query? Yes, we can always eager load our relationship in laravel application. There is a model property called $with and using it we can laravel eloquent always load relation. 

So in this tutorial, we will see how to load relationship data without calling it from the controller in laravel. I will use $with the inside model and we have to pass the relationship method name which you want to eager load. 

laravel-eloquent-always-load-relation

Now let's see the way of laravel eloquent always load relation. Let's first update the Post model like this:

app/Models/Post.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Post extends Model
{
    use HasFactory;

    protected $with = ['comments'];

    public function comments()
    {
        return $this->hasMany(Comment::class);
    }
}

 

Now you can fetch all comments of every post this way. No need to call for comments. 

app/Http/Controllers/TutorialController.php

<?php

namespace App\Http\Controllers;

use App\Models\Post;

class TutorialController extends Controller
{   
    /**
     * Tutorial from LARAVELIA
     */
    public function index()
    {   
        return Post::all();
    }
}

 

Now see the output:

[
    {
        "id": 1,
        "user_id": 1,
        "title": "Omnis cupiditate sed quia tempora. Nulla eum debitis molestias porro aut enim accusantium. Quia aut numquam illo et. Error magnam illum in qui et placeat at soluta.",
        "post_views": 12,
        "is_published": 1,
        "created_at": "2023-01-29T08:49:59.000000Z",
        "updated_at": "2023-01-29T08:49:59.000000Z",
        "comments": [
        {
            "id": 1,
            "post_id": 1,
            "user_id": 1,
            "comment": "Officia placeat dolorem magni fugiat dolore voluptate.",
            "vote": 41,
            "is_approved": 0,
            "created_at": "2023-01-29T08:49:59.000000Z",
            "updated_at": "2023-01-29T08:49:59.000000Z"
        },
        {
            "id": 2,
            "post_id": 1,
            "user_id": 1,
            "comment": "Iste praesentium soluta quo qui deleniti explicabo qui pariatur.",
            "vote": 28,
            "is_approved": 0,
            "created_at": "2023-01-29T08:49:59.000000Z",
            "updated_at": "2023-01-29T08:49:59.000000Z"
        }
        ]
    },
    {
        "id": 2,
        "user_id": 2,
        "title": "Enim sed pariatur veritatis animi. Odit quaerat beatae excepturi quaerat.",
        "post_views": 30,
        "is_published": 1,
        "created_at": "2023-01-29T08:49:59.000000Z",
        "updated_at": "2023-01-29T08:49:59.000000Z",
        "comments": [
            {
            "id": 3,
            "post_id": 2,
            "user_id": 2,
            "comment": "Voluptate ducimus consequatur et vero unde veritatis ea.",
            "vote": 10,
            "is_approved": 0,
            "created_at": "2023-01-29T08:49:59.000000Z",
            "updated_at": "2023-01-29T08:49:59.000000Z"
            }
        ]
    }
]

 

Read also: Laravel Tree Relationship - Laravel Hierarchical Relationship

 

Conclusion

In this laravel eloquent always load relation tutorial,  I have tried my best to let you know always load eager loading laravel. Hope now after completing this load relationship data without calling it from controller in laravel tutorial, you will know how to dynamically load relationship result in laravel.