How to use has one through in laravel?

Stack Overflow Asked by ahz on September 17, 2020

I want to ask, there are three tables I’ve created, the following tables is bellow


  • brand_id
  • brand_name


  • motorcycle_id
  • brand_id
  • motorcycle_name


  • purchase_id
  • motorcycle_id
  • purchase_price
  • purchase_location

How to get data purchase_id, motorcycle_name and brand_name ?
I tried to use hasOneThrough() with model like this :

class Purchase extends Model
    protected $primaryKey = 'purchase_id';

    public function motorcycleBrand(){
        return $this->hasOneThrough(Brand::class,Motorcycle::class,'brand_id','brand_id');

And this is code inside controller :

$data = Purchase::->with(['motorcycleBrand', 'motorcycle'])->get();
return response()->json($data);

But it’s not working, motorcycle data is showing but brand data is not showing (null).
Is it possible if I want to get data as I want ? If possible how to get it ?
Thank you..

Add your own answers!

Related Questions

bootstrap collapsible div disable

1  Asked on December 11, 2021 by kumara


Is there any way to access an internal Node.js module?

1  Asked on December 11, 2021 by vilicvane


Issues with matplotlib

1  Asked on December 11, 2021 by giancarlo-betti


Import a list of string from another file in flutter

1  Asked on December 11, 2021 by wytex-system


Gradle Issue when trying to add local library

1  Asked on December 11, 2021 by ilovesomething23


Django Formset Initialization in POST and GET

0  Asked on December 11, 2021 by aannaa


Penalty for Non-State-Changing Texture Rebinding in OpenGL

0  Asked on December 11, 2021 by pprovins


MongoDB aggregate Query from mongodb university course

0  Asked on December 11, 2021 by 1005hoon


Classes and interfaces in Angular 9

1  Asked on December 11, 2021 by harry_c


Android Studio. App crashes after thread sleeps

1  Asked on December 9, 2021 by lfc-productions


Ask a Question

Get help from others!

© 2022 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP