【问题标题】:BelongsTo relationship working on local but not on productionBelongsTo 关系在本地工作但不在生产上
【发布时间】:2019-11-12 14:31:44
【问题描述】:

我有两个表庄园和平面图的 belongsTo 和 HasMany 关系,一切都适用于我的本地但不适用于生产,

我已经尝试查看表格输入,即使清除和重新添加似乎没有任何效果。

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');

}}

购买属性模型

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


}}

控制器代码

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

Blade 遇到的错误

Trying to get property 'd3floor_plan_img_sale' of non-object 

如果有人能指出我正确的方向,我将不胜感激。提前致谢

更新 - 查看 Tinker 结果

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,**
   },

**来自本地开发环境的相同修补程序结果**

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",                                    
      },                                                
     },

【问题讨论】:

  • 结论很可能您正在访问其中 Estate_id 为空的行
  • 添加了修补结果, Estate_id 不为空
  • 但是你所有的关系都绑定在 Estate_id 上,这似乎是错误的,生产中的 d3floorplans 的 id 是什么?
  • 我的模型上有多个外键,d3floorplans 的 id 也是 ID,加上 3 个引用其他表的外键。我最终通过使用stackoverflow.com/questions/48077890/… 线程解决了这个问题。谢谢

标签: laravel eloquent laravel-6


【解决方案1】:

我能够通过安装 Compoships https://github.com/topclaudy/compoships 并在我的模型中配置它来解决这个错误,因为我的所有模型上都有多个相互引用的外键。

如果有人偶然发现同样的问题,您也可以参考 Laravel Eloquent: multiple foreign keys for relationship 这也很有帮助。

谢谢

【讨论】:

    猜你喜欢
    • 2018-08-01
    • 2016-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 2021-09-21
    • 2016-02-18
    • 2019-06-13
    相关资源
    最近更新 更多