Php Laravel 5-没有显示与belongsTo的关系

Php Laravel 5-没有显示与belongsTo的关系,php,laravel,laravel-5,eloquent,Php,Laravel,Laravel 5,Eloquent,我已经有一段时间没有参与Laravel项目了,我不明白为什么会发生这个问题 我有一张“车辆”表和一张“客户”表 在vehicles表中,有一行的字段“customer_id”的值为1。“customers”表中还有一个id为1的客户 在我的车型中,我有: public function customer() { return $this->belongsTo('App\Customer'); } 但从查询返回数据时: $vehicle = Vehicle::find($id);

我已经有一段时间没有参与Laravel项目了,我不明白为什么会发生这个问题

我有一张“车辆”表和一张“客户”表

在vehicles表中,有一行的字段“customer_id”的值为1。“customers”表中还有一个id为1的客户

在我的车型中,我有:

public function customer()
{
    return $this->belongsTo('App\Customer');
}
但从查询返回数据时:

$vehicle = Vehicle::find($id);
dd($vehicle);
关系数组为空


我在这件事上的错误在哪里?没有错误,只是没有数据。

如果您想让客户使用车辆:

$vehicle = Vehicle::with('customer')->find($id);

如果您想让客户使用车辆:

$vehicle = Vehicle::with('customer')->find($id);

如果你像这样定义你的关系,假设这是一对多关系,不是更好吗

在车辆模型中:

public function customer()
{
    return $this->belongsTo('App\Customer', 'id', 'customer_id');
}
在客户模型中

public function vehicle()
{
    return $this->hasOne('App\Vehicle', 'customer_id', 'id');
}
然后装上你需要的东西

$vehicle = Vehicle::find($id); 
但只有在调用时才会加载关系:

$vehicle->customer;

如果你像这样定义你的关系,假设这是一对多的关系,不是更好吗

在车辆模型中:

public function customer()
{
    return $this->belongsTo('App\Customer', 'id', 'customer_id');
}
在客户模型中

public function vehicle()
{
    return $this->hasOne('App\Vehicle', 'customer_id', 'id');
}
然后装上你需要的东西

$vehicle = Vehicle::find($id); 
但只有在调用时才会加载关系:

$vehicle->customer;

您没有加载(或急切或懒惰)relationNice建议您没有加载(或急切或懒惰)relationNice建议,但在这种情况下,一辆车可能只属于一个客户,但一个客户可以有许多不同的车(如果他们购买多个)Nice建议,但在这种情况下,一辆车只能属于一个客户,但一个客户可以拥有许多不同的车辆(如果他们购买多辆)