简体   繁体   English

获取子项 Laravel Model 的第二个父项

[英]Get second parent of child item Laravel Model

I have 3 model.我有 3 个 model。

  1. Room - columns: id, name, isactive.房间 - 列:id、name、isactive。

  2. Sensor - columns: id, name, isactive.传感器 - 列:id、name、isactive。

  3. RoomSensors - columns: id, roomid, sensorid, isactive. RoomSensors - 列:id、roomid、sensorid、isactive。

I have models for all of them.我有他们所有人的模型。

Room房间

class WarehouseRoom extends Model
{
    protected $table = 'tempohub_rooms';

    protected $fillable = ['id','warehouseid','name','isactive','image'];

    public    $timestamps   = false;

    public function warehouse_roomsensors()
    {
        return $this -> hasMany('App\WarehouseRoomSensor','roomid');
    }

    public function warehouse()
    {
        return $this -> belongsTo('App\Warehouse','warehouseid','id');
    }
}

Sensor传感器

class Sensor extends Model
{
    protected $table = 'tempohub_sensors';

    protected $fillable = [];

    public function roomToSensor() {
        return $this -> hasMany('App\WarehouseRoomSensor', 'sensorid');
    }
}

RoomSensors房间传感器

class WarehouseRoomSensor extends Model
{
    protected $table = 'tempohub_roomsensors';

    protected $fillable = [];

    public    $timestamps   = false;

    public function sensor() 
    {
        return $this -> belongsTo('App\Sensor', 'sensorid', 'id');
    }

    public function room()
    {
        return $this -> belongsTo('App\WarehouseRoom','roomid','id');
    }

}

The page is not written by me, so I have to continue as it was made.页面不是我写的,所以我必须照原样继续。 And in blade I have the loop.在刀片中,我有循环。

@foreach($warehouse_room -> warehouse_roomsensors -> sortBy('index') as $sensor)

It must give me the info about sensor on the rooms, but it cant.它必须给我有关房间传感器的信息,但它不能。 So I need to get Warehouse_room -> Warehouse_roomsensor -> Warehouse_sensor所以我需要获取 Warehouse_room -> Warehouse_roomsensor -> Warehouse_sensor

You need to add a new many-to-many relationship in your room model.您需要在房间 model 中添加新的多对多关系。

Many-to-many relationships are defined by writing a method that returns the result of the belongsToMany method.多对多关系是通过编写一个返回belongsToMany方法的结果的方法来定义的。 The belongsToMany method is provided by the Illuminate\Database\Eloquent\Model base class that is used by all of your application's Eloquent models. belongsToMany方法由Illuminate\Database\Eloquent\Model基础 class 提供,您的应用程序的所有 Eloquent 模型都使用它。 For example, let's define a sensors method on our WarehouseRoom model.例如,让我们在 WarehouseRoom model 上定义一个传感器方法。 The first argument passed to this method is the name of the related model class and the second argument would be the name of the intermediate table.:传递给此方法的第一个参数是相关 model class 的名称,第二个参数是中间表的名称。:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class WarehouseRoom extends Model
{
    /**
     * The sensors that belong to the room.
     */
    public function sensors()
    {
        return $this->belongsToMany(Sensors::class, 'tempohub_roomsensors');
    }
}

Once the relationship is defined, you may access the room's sensors using the sensors dynamic relationship property:定义关系后,您可以使用sensors动态关系属性访问房间的传感器:

use App\Models\WarehouseRoom;

$warehouseRoom = WarehouseRoom::find(1);

foreach ($warehouseRoom->sensors as $sensor) {
    //
}

In blade在刀片

@foreach($warehouseRoom->sensors as $sensor)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM