如何在laravel中实现这种关系?

时间:2017-11-04 11:09:26

标签: laravel polymorphism relationship

我有4张桌子

inst
   id
   subject

report
   id
   subject

 object
   id
   type

 container
    id
    obj_id

我想用容器表的obj_id,实现相应的对象记录。 然后,如果object的类型= 1,则从inst table或type = 2获取数据,从报告表中获取数据

2 个答案:

答案 0 :(得分:1)

根据您的信息,

你可以像@Mahfuz Shishir一样回答

在对象模型

中创建新的附加属性
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="form-group"><label class="sr-only" for="id_max_size">Max 
   Size</label><select name="max_size" title="" required class="form-control" id="id_max_size">
       <option value="" disabled selected>Size ↓</option>
       <option value="10">AAA</option>
       <option value="20">BBB</option>
       <option value="30">CCC</option>
       <option value="40">DDD</option>
    </select>
</div>

<input type="number" name="no_needed" title="" required id="id_no_needed" step="0.01" />
<input type="number" name="total_price" title="" required id="id_total_price" step="0.01" />

答案 1 :(得分:0)

关系看起来像。

在您的Object.php模型中

@override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(title: new Text("ListTile Example"),),
        body: new ListView(
          children: new List.generate(7, (int index) {
            return new Container(
              padding: const EdgeInsets.symmetric(
                  vertical: 10.0, horizontal: 20.0),
              child: new Column(
                children: <Widget>[
                  new Align (child: new Text("Centered Title $index",
                    style: new TextStyle(fontSize: 40.0),), //so big text
                    alignment: FractionalOffset.topLeft,),
                  new Divider(color: Colors.blue,),
                  new Align (child: new Text("Subtitle $index"),
                    alignment: FractionalOffset.topLeft,),
                  new Divider(color: Colors.blue,),
                  new Align (child: new Text("More stuff $index"),
                    alignment: FractionalOffset.topLeft,),
                  new Row(mainAxisAlignment: MainAxisAlignment.end,
                    children: <Widget>[ //add some actions, icons...etc
                      new FlatButton(onPressed: () {}, child: new Text("EDIT")), 
                      new FlatButton(onPressed: () {},
                          child: new Text("DELETE",
                            style: new TextStyle(color: Colors.redAccent),))
                    ],),
                ],
              ),
            );
          }),
        )
    );
  }

在你的Container.php模型中

public function containers()
{
    return $this->hasMany('App\Container', 'foreign_id');
}

您提供的信息很少。根据您自己的意义实现其余的控制器代码。或者放一些你尝试过的代码。