如何在Laravel中选择外键值

4

我有两个表格,一个是名为car_category的表格,包含字段-id,type。另一个名为vehicle的表格,包含字段-c_id(FK引用car - id)。 现在我想显示FK(c_id)的值,即car-type。 我在models中有以下代码:

class Car extends Model
{
    protected $guarded = [];
    protected $table = 'car_category';

    public function vehicles()
    {
        return $this->hasMany('Vehicle');
    }
}

车型。
class Vehicle extends Model
{
    protected $guarded = [];
    protected $table = 'vehicles';
    public function cars()
    {
        return $this->belongsTo('Car');
    }
}

这将是我的查询吗?我已尝试过这段代码,结果出现错误。
$vehicles = "SELECT cars.cartype,vehicles.model FROM cars,vehicles 
             WHERE cars.id = vehicles.c_id";

我该怎么做才能实现这个?有人能帮我吗?


你的类别应该是Car - Jigar Shah
查询有没有错误? - usertest
我对你的表格感到困惑,在查询中你有 carsvehicles,而模型中有 car_categoryvehicles - Jigar Shah
carsvehicles是关系模型的函数名称。 - usertest
4个回答

7

试试这个

class Car extends Model  
{
protected $guarded = [];
protected $table = 'car_category';

 public function vehicles()
 {
   return $this->hasMany(Vehicle::class, 'c_id');
 }
}

车辆型号
 class Vehicle extends Model
 {
  protected $guarded = [];
  protected $table = 'vehicles';

  public function cars()
  {
    return $this->belongsTo(Car::class, 'c_id');
  }
 }

Eloquent 根据模型名称确定关系的外键。在这种情况下,Car 模型自动假定具有 car_id 外键。如果您希望覆盖此约定,可以将第二个参数传递给该方法。

https://laravel.com/docs/5.5/eloquent-relationships#one-to-one

使用 Eager Loading 可以获取车辆及其车辆信息的查询结果。
$result = Car::with('vehicles')->get();

谢谢,但是查询语句应该是什么? - usertest
要获取汽车及其车辆信息,您可以使用Eager Loading进行查询。$result = Car::with('vehicles')->get(); - Bivin
好的,使用 $result->vehicles->car_type; 获取车辆表的数据,对吧? - usertest

1
要获取包括其车辆信息的“Car”,您可以使用Eager Loading进行查询。
$result = Car::with('vehicles')->get();

您需要进行一次更正,您已经将类名指定为字符串字面量,而没有指定FQN。模型中的关系应该使用完全限定名称来定义。
汽车模型
class Car extends Model
{
    protected $guarded = [];
    protected $table = 'car_category';

    public function vehicles()
    {
        return $this->hasMany(\App\Models\Vehicle::class);
    }
}

汽车型号
class Vehicle extends Model
{
    protected $guarded = [];
    protected $table = 'vehicles';
    public function cars()
    {
        return $this->belongsTo(\App\Models\Car::class);
    }
}

在HasRelationships.php(第487行)中找不到“Vehicle”类。 - usertest
@saranya 我已经更新了我的答案。如果你在模型中使用了不同的命名空间而不是 App\Models,那么我建议你在定义关系时放置正确的命名空间。 - M Khalid Junaid
好的。如何在视图中获取表格数据?是通过 $result->vehicles->car_type; 对吧? - usertest
@saranya 是的,将其传递给视图,然后循环遍历您的数据,然后针对每辆车循环遍历 vehicles - M Khalid Junaid
当我执行了查询时,它只返回了“car”表的数据。 - usertest
在视图中显示错误:属性[id]在此集合实例上不存在。(视图:C:\xampp\htdocs\laravel\movecab\resources\views\vehicles\vehicle.blade.php) - usertest

0

class car 改为 class Car

之后,您可以使用 Car::first() 进行选择,相关表格数据可以在 Car::first()->vehicles 中找到

如果您有多条记录,还可以在模型上添加 where() 方法,使用 foreach()。


你能给我一个示例样本吗? - usertest

0
In Model,
 class Vehicle extends Model
 {
protected $guarded = [];
protected $table = 'vehicles';

 public function cars()
{
 return $this->belongsTo(Car::class, 'c_id');
}
}

在控制器中,

    $vehicles = Vehicle::all();
    return view('vehicles.vehicle',['vehicles'=>$vehicles]);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接