Hi Devina,
Have you tried eager loading the nested relationship like so:
$mainservices = MainService::with([service.category])->get();
That should do the trick.
Jim
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.
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
}
},
.
.
.
]
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community