【问题标题】:Retrieve all data using eloquent laravel使用 eloquent laravel 检索所有数据
【发布时间】:2019-03-04 14:05:11
【问题描述】:

想问一下laravel中如何获取所有数据

我有这样的表

公司

corp_id
corp_name

子公司

subsidiary_id
corp_id
subsidiary_name

客户图片

cust_pic_id
corp_id
subsidiary_id
cust_name
cust_phone
cust_address
cust_email

每个模型都这样

公司模型

protected $table= 'corps';
public function CustomerPIC()
{
    return $this->hasMany('App\CustomerPIC');
}

子公司模式

protected $table = 'subsidiaries';

public function CustomerPIC()
{
    return $this->hasMany('App\CustomerPIC');
}

客户 PIC 模型

protected $table = 'customer_P_I_CS';

public function corp()
{
    return $this->belongsTo('App\Corp');
}

public function subsidiary()
{
    return $this->belongsTo('App\Subsidiary');
}

我有一个控制器

public function getCustomer()
{
    $getData = CustomerPIC::with(['Corp','Subsidiary'])->get();


    return response()->json([
        'data'=>$getData
    ]);
}

问题是,为什么我在没有corp_name 和subsistant_name 的情况下得到回报。还有 corp:NULL 和 subsub: NULL 即使我没有那一行

哪一个没有corp_name和substance_name?

我想返回所有数据。

感谢阅读

【问题讨论】:

    标签: mysql database join eloquent


    【解决方案1】:

    确保您使用小写字母加载关系(与您定义的关系相同):

    $getData = CustomerPIC::with(['corp','subsidiary'])->get();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-06
      • 2019-12-11
      相关资源
      最近更新 更多