有一个通过Laravel雄辩 [英] Has one through Laravel Eloquent

查看:112
本文介绍了有一个通过Laravel雄辩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三张表车库汽车证券

证券是保持一辆汽车安全的证券,您可以拥有多个安全,但是一个安全保障只能保护一辆汽车。车库是汽车的地方,证券也是这样。

The securities are the ones that is keeping one car safe, you can have more than one security, but a single security can keep only one car safe. The garage is where the car is and the securities are as well.

我想要的是列出所有的证券,知道车库的名称。问题是,证券没有包含车库的ID的列,只有汽车的ID保持安全,但是汽车的ID为车库。

What I want is to list all the securities and know the name of the garage that he is. The problem is that securities doesn't have a column containing the id of the garage, only the id of the car that he is keeping safe, but car has the id of the garage.

Laravel雄辩有一种叫做 hasManyThrough 的方法,但证券只有一个车库通过汽车

Laravel Eloquent has a method called hasManyThrough, but securities has one garage through cars only.

这里是表:

车库表:

-----------------------------------
|garage_id|garage_name|garage_size|
-----------------------------------
|        1|   Garage 1|        200|
-----------------------------------
|        2|   Garage 2|        400|
-----------------------------------

汽车表:

---------------------------
|car_id|car_name|garage_id|
---------------------------
|     1|   Car 1|        1|
---------------------------
|     2|   Car 2|        1|
---------------------------
|     3|   Car 3|        2|
---------------------------

证券表:

----------------------------------
|security_id|security_name|car_id|
----------------------------------
|          1|  Security 1|      1|
----------------------------------
|          2|  Security 2|      1|
----------------------------------
|          3|  Security 3|      2|
----------------------------------
|          4|  Security 4|      3|
----------------------------------

输出必须是:

Security 1 is on Garage 1
Security 2 is on Garage 1
Security 3 is on Garage 1
Security 4 is on Garage 2

我有以下模型:

代码是列出车库,但是我想做相似,但要列出证券(只是为了让你了解结构是如何的)。

The code is to list the garages, but I want to make similar but to list the securities (just for you to have an idea of how the structure is).

$garages = Garages::with(['cars', 'securities'])->get();

$garages->transform(function($garages) {
    return array(
        'garage_name'      => $garage->garage_name,
        'count_cars'       => $garage->cars->count(),
        'count_securities' => $garage->securities->count(),
   );
});

class Garages extends Eloquent
{
    public function cars()
    {
        return $this->hasMany('cars');
    }

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

class Cars extends Eloquent
{
}

class Securities extends Eloquent
{
}

只是再次强调:我想要的名字与汽车相关的车库,安全性保持安全。

只是为了使它更容易理解,如果我这样做:

Just to make it even easier to understand, if I do this:

$securities = Securities::with(['cars'])->get();

class Securities extends Eloquent
{
    public function cars()
    {
        return $this->hasOne('cars');
    }
}

我将只收到 cars 表中的garage_id 作为关系。我真正想要的是车库的名称。

I will get only the garage_id from cars table as relations. What I really want is the name of the garage.

[relations:protected] => Array
    (
        [cars] => Cars Object
            (
                ...
                [attributes:protected] => Array
                    (
                        ...
                        [car_name] => Car 1
                        [garage_id] => 1
                        ...


推荐答案

看起来你没有正确地关联对象,让我们把它拆分下来:

It looks like you are not relating objects correctly. Let's break that down:

如果一个 Garage 有很多 Car 然后一个 Car 属于 Garage ,请记住这个想法。

If a Garage Has Many Car then a Car Belongs To Garage, Lets proceed with this idea in mind.


  • 车库有许多汽车

  • 汽车有许多安全性

  • 安全性属于 Car

  • 车库有许多安全性通过汽车

  • Garage Has Many Car
  • Car Has Many Security
  • Security Belongs To Car
  • Garage Has Many Security Through Car

在雄辩中你可以j如果你打开这些关系,应该在上面发布的模式中工作。

In Eloquent you can just go ahead and clap those relations in, it should work given the schema you posted above.

class Garage extends Eloquent
{
    public function cars()
    {
        return $this->hasMany('cars');
    }

    public function securities()
    {
        return $this->hasManyThrough('Security', 'Car');
    }
}

class Car extends Eloquent
{
    public function securities()
    {
        return $this->hasMany('Security');
    }

    // ADDED IN SECOND EDIT

    public function garage()
    {
        return $this->belongsTo('Garage');
    }       
}

class Security extends Eloquent
{
    public function car()
    {
        return $this->belongsTo('Car');
    }
}

应该是这样的。

编辑:只要有一个路径,您可以使用这些关系的组合从一个模型绘制到另一个模型,您可以访问所有这些关系。检查出来,例如:

You can access all these relations from any model as long as there is a path you can draw from one model to another using a combination of these relations. Check this out for example:

$security = Security::with('car.garage')->first();

将首先检索安全性记录并加载 Car 关系,然后再进一步加载每个 Car的所有 Garage 关系对象加载在安全性模型下。

will retrieve first Security record and load Car relation on it, then it goes one more step and load all Garage relations on every Car object loaded under Security model.

您可以使用以下语法访问它们: / p>

You can access them using this syntax:

$security->car->garage->id

// Other examples
$garage = Garage::with('cars.securities')->first();

foreach($garage->cars as $car)
{
    foreach($cars->securities as $security)
    {
        echo "Car {$car->id} has {$security->id} assigned to it";
    }
}

此外,请注意,关系对象是收集,所以你有所有的花哨的方法,如 - > each() > count() - > map()

Furthermore, notice that the relationship objects are an instance of Collection so you have all the fancy methods such as ->each(), ->count(), ->map() available on them.

这篇关于有一个通过Laravel雄辩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆