0

I have a belongsTo and HasMany relationship for two tables estates and floorplan, everthing works on my local but not on production,

I have tried looking at table inputs even clearing and re-adding nothing seems to work.

Model for d3floorplan

class d3floorplan extends Model {
// 
protected $table = 'd3floorplan';

public function estates()
{
    return $this->belongsTo('id',estates::class);

}
public function estate_house_types(){
    return $this->hasMany(estate_house_types::class,'estate_id','house_type_id');

}
public function buyproperty(){
    return $this->hasMany(buyproperty::class,'estate_id','house_type_id');

}}

Buyproperty model

class buyproperty extends Model{
protected $table = 'buyproperty';
public function d3floorplan(){
    return $this->belongsTo(d3floorplan::class, 'estate_id', 'id');


}}

Controller Code

$getproperties = buyproperty::with('d3floorplan')->get();

Error Experienced on Blade

Trying to get property 'd3floor_plan_img_sale' of non-object 

I will be grateful if anyone could point me in the right direction. Thanks in advance

Update - See Tinker results

App\buyproperty {#3016
     id: 35,
     created_at: "2019-11-12 13:37:06",
     updated_at: "2019-11-12 13:37:06",
     estate_id: 2,
     block_id: 157,
     house_type_id: 26,
     kitchen_id: 94,
     tile_id: 42,
     floor_id: 61,
     customer_name: "skufndff fdf",
     customer_email: "4343@aol.com",
     mobile_number: "0939023902",
     age_bracket: "22",
     accept_terms: 1,
     Amount_Paid: "323232",
     payment_status: "1",
     estates: App\estates {#3022
       id: 2,
       created_at: "2019-11-03 16:36:33",
       updated_at: "2019-11-03 16:36:33",
       Estate_name: "Blueocen",
       Estate_logo: "BlueLOGO.jpg",
       status: "1",
       display_pic: Blueocean3.jpg",
     },
     **d3floorplan: null,**
   },

**Same Tinker Results from local Dev environment **

App\buyproperty {#3016
     id: 35,
     created_at: "2019-11-12 13:37:06",
     updated_at: "2019-11-12 13:37:06",
     estate_id: 2,
     block_id: 157,
     house_type_id: 26,
     kitchen_id: 94,
     tile_id: 42,
     floor_id: 61,
     customer_name: "skufndff fdf",
     customer_email: "4343@aol.com",
     mobile_number: "0939023902",
     age_bracket: "22",
     accept_terms: 1,
     Amount_Paid: "323232",
     payment_status: "1",
     estates: App\estates {#3022
       id: 2,
       created_at: "2019-11-03 16:36:33",
       updated_at: "2019-11-03 16:36:33",
       Estate_name: "Blueocen",
       Estate_logo: "BlueLOGO.jpg",
       status: "1",
       display_pic: Blueocean3.jpg",
     },
     estate_blocks: null,
     estate_house_types: null,
     estate_lookup_house_types: null,
     d3floorplan: App\d3floorplan 
        {#3039               
        id: 2,                                          
        created_at: "2019-10-09 21:53:14",              
        updated_at: "2019-10-09 21:53:16",              
        estate_id: 2,                                   
        house_type_id: 1,                               
       d3floorplan_title: "Test Title",                
       d3floor_plan_img: "2brfairfield-bdr-01.png",    
       d3floor_plan_img_sale: "1bedsale.jpg",          
      status: "1",                                    
      },                                                
     },
Zee4real
  • 17
  • 5
  • Conclusion most likely you are accessing a row where estate_id is null – mrhn Nov 12 '19 at 14:40
  • Added tinker results, the estate_id is not null – Zee4real Nov 12 '19 at 14:51
  • But all your relations is bound on estate_id that seems wrong, what are the ids of the d3floorplans in production? – mrhn Nov 12 '19 at 18:31
  • I had multiple foreign keys on my models, the id for d3floorplans is also ID, plus 3 forigen keys referencing other tables. i eventually solved the problem by using the https://stackoverflow.com/questions/48077890/laravel-eloquent-multiple-foreign-keys-for-relationship thread. thanks – Zee4real Nov 13 '19 at 12:46

1 Answers1

0

I was able to resolve the bug by installing Compoships https://github.com/topclaudy/compoships and configuring it in my models, because i had mutitple foreign keys on all my models referencing each other.

Incase anyone stumbles upon this same issue you can also refer to Laravel Eloquent: multiple foreign keys for relationship this also helped alot.

Thanks

Zee4real
  • 17
  • 5