Support the ongoing development of Laravel.io →
Eloquent Laravel
Last updated 2 years ago.
0

Hi Devina,

Have you tried eager loading the nested relationship like so:

$mainservices = MainService::with([service.category])->get();

That should do the trick.

Jim

0

hey @Devina,

once you've defined a relationship between two models you have to call the function of the relationship and then you should specify which item(s) you want i.e(first, all, get+where ..etc), so your code should be something like :

$service = $mainservice->service()->first();
$category = $service->category()->first();

if your code is working as you mentioned for service data with

$services = $mainservice->service;

that means your Mainservice model has a column called service & its not reading from Service model.

0

Hi @Huthayfa my Mainservice model doesn't have column called service, it's extended from users table, here my

User/Mainservice migration


Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->string('user_code', 11);
    $table->string('admin_code', 6)->nullable();
    $table->string('full_name', 255);
    $table->string('email', 255)->unique();
    $table->string('password', 255);
    $table->string('gender', 1); 
    $table->string('phone');
    $table->string('profile_image', 255);
    $table->string('verification_link', 255)->nullable();
    $table->string('reset_password', 255)->nullable(); 
    $table->string('verified', 1)->default(User::UNVERIFIED_USER);
    $table->string('admin', 1)->default(User::REGULER_USER);; 
    $table->rememberToken();
    $table->timestamps();
    $table->softDeletes();
});

and the JSON Response from Postman


{
    "data": [
        {
            "id": 1,
            "user_code": "Yt3LRfzB93E",
            "admin_code": null,
            "full_name": "Dr. Holden Marvin Iv",
            "email": "[email protected]",
            "gender": "1",
            "phone": "71641872939683",
            "profile_image": "/tmp/13b73edae8443990be1aa8f1a483bc27.jpg",
            "verified": "1",
            "admin": "0",
            "service": {
                "id": 94,
                "main_service_id": 1,
                "service_code": "OjXUhRZATTw",
                "ktp_image": "/tmp/13b73edae8443990be1aa8f1a483bc27.jpg",
                "sim_image": "/tmp/13b73edae8443990be1aa8f1a483bc27.jpg",
                "stnk_image": "/tmp/13b73edae8443990be1aa8f1a483bc27.jpg",
                "vehicle_image": "/tmp/13b73edae8443990be1aa8f1a483bc27.jpg",
                "license_platenumber": "LfCvBynV",
                "verified": "1",
                "setting_mode": "1",
                "vehicle_type": "neque",
                "category_id": 11
            }
        },
        .
        .
        .
   ]
}
Last updated 7 years ago.
0

Hi @jimmcm88, Thank you so much, the result is what I expected :)

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Devina shilohchis Joined 22 Jun 2017

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.